summaryrefslogtreecommitdiffhomepage
path: root/nGenEx/AviFile.cpp
blob: b9c092bb46c029b25e8a7e40165ed70329801501 (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
/*  Project nGenEx
	Destroyer Studios LLC
	Copyright © 1997-2004. All Rights Reserved.

	SUBSYSTEM:    nGenEx.lib
	FILE:         AviFile.cpp
	AUTHOR:       John DiCamillo


	OVERVIEW
	========
	AviFile reader / writer
*/


#include "MemDebug.h"
#include "AviFile.h"

#include <vfw.h>
#include <stdio.h>
#include <stdlib.h>

// +--------------------------------------------------------------------+

void Print(const char* fmt, ...);

// +--------------------------------------------------------------------+

AviFile::AviFile(const char* fname)
: filename(fname), fps(0), play(true), pfile(0), ps(0), ps_comp(0),
nframe(0), nsamp(0), iserr(false), frame_size(0)
{
	AVIFileInit();
}

AviFile::AviFile(const char* fname, const Rect& r, int frame_rate)
: filename(fname), rect(r), fps(frame_rate), play(false), pfile(0),
ps(0), ps_comp(0), nframe(0), nsamp(0), iserr(false)
{
	Print("\n\nAviFile(%s, w=%d, h=%d, f=%d FPS)\n", fname, r.w, r.h, fps);
	frame_size = r.w * r.h * 3;

	AVIFileInit();
	HRESULT hr = AVIFileOpen(&pfile, fname, OF_WRITE|OF_CREATE, 0);

	if (hr != AVIERR_OK) {
		Print("AviFile - open failed. %08x\n", hr);
		iserr = true;
		return;
	}

	Print("AviFile - open successful\n");

	AVISTREAMINFO strhdr; 
	ZeroMemory(&strhdr,sizeof(strhdr));

	strhdr.fccType    = streamtypeVIDEO;
	strhdr.fccHandler = 0; 
	strhdr.dwScale    = 1000 / fps;
	strhdr.dwRate     = 1000;
	strhdr.dwSuggestedBufferSize  = frame_size;

	SetRect(&strhdr.rcFrame, 0, 0, r.w, r.h);

	hr = AVIFileCreateStream(pfile, &ps, &strhdr);

	if (hr != AVIERR_OK) {
		Print("AviFile - create stream failed. %08x\n", hr);
		iserr = true;
		return;
	}

	Print("AviFile - create stream successful\n");

	AVICOMPRESSOPTIONS opts; 
	ZeroMemory(&opts,sizeof(opts));
	opts.fccType      = streamtypeVIDEO;
	//opts.fccHandler = mmioFOURCC('W','M','V','3');
	opts.fccHandler   = mmioFOURCC('D','I','B',' ');  // (full frames)
	opts.dwFlags      = 8;

	hr = AVIMakeCompressedStream(&ps_comp, ps, &opts, 0);
	if (hr != AVIERR_OK) {
		Print("AviFile - compressed stream failed. %08x\n", hr);
		iserr=true; 
		return;
	}

	Print("AviFile - make compressed stream successful\n");

	BITMAPINFOHEADER bmih;
	ZeroMemory(&bmih, sizeof(BITMAPINFOHEADER));

	bmih.biSize          = sizeof(BITMAPINFOHEADER);
	bmih.biBitCount      = 24;
	bmih.biCompression   = BI_RGB;
	bmih.biWidth         = rect.w;
	bmih.biHeight        = rect.h;
	bmih.biPlanes        = 1;
	bmih.biSizeImage     = frame_size;

	hr = AVIStreamSetFormat(ps_comp, 0, &bmih, sizeof(BITMAPINFOHEADER));

	if (hr != AVIERR_OK) {
		Print("AviFile - stream format failed. %08x\n", hr);
		iserr=true; 
		return;
	}

	Print("AviFile - stream format successful\n");
}

AviFile::~AviFile()
{
	if (!play) {
		Print("*** Closing AVI file '%s' with %d frames\n", (const char*) filename, nframe);
	}

	if (ps_comp)   AVIStreamRelease(ps_comp);
	if (ps)        AVIStreamRelease(ps);
	if (pfile)     AVIFileRelease(pfile);

	AVIFileExit();
}

// +--------------------------------------------------------------------+
//
// Note that AVI frames use DIB format - Y is inverted.
// So we need to flip the native bmp before sending to the
// file.

HRESULT
AviFile::AddFrame(const Bitmap& bmp)
{
	HRESULT hr = E_FAIL;

	if (!iserr && !play && bmp.IsHighColor() && bmp.Width() == rect.w && bmp.Height() == rect.h) {
		int      w      = bmp.Width();
		int      h      = bmp.Height();
		BYTE*    buffer = new(__FILE__,__LINE__) BYTE[frame_size];
		BYTE*    dst    = buffer;

		for (int y = 0; y < bmp.Height(); y++) {
			Color* src = bmp.HiPixels()  + (h - 1 - y) * w;

			for (int x = 0; x < bmp.Width(); x++) {
				*dst++ = (BYTE) src->Blue();
				*dst++ = (BYTE) src->Green();
				*dst++ = (BYTE) src->Red();
				src++;
			}
		}

		hr = AVIStreamWrite(ps_comp, nframe, 1, buffer, frame_size, AVIIF_KEYFRAME, 0, 0);
		
		if (SUCCEEDED(hr)) {
			nframe++;
		}
		else {
			Print("AVIStreamWriteFile failed. %08x\n", hr);
			iserr = true;
		}

		delete [] buffer;
	}

	return hr;
}

// +--------------------------------------------------------------------+

HRESULT
AviFile::GetFrame(double seconds, Bitmap& bmp)
{
	HRESULT hr = E_FAIL;
	return hr;
}