/
QTVideo.c
executable file
·227 lines (181 loc) · 6.76 KB
/
QTVideo.c
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
/*
File: QTVideo.c
Contains: Code to create video tracks for QuickTime CreateMovie sample
Written by: Scott Kuechle
(based heavily on QuickTime sample code in Inside Macintosh: QuickTime)
Copyright: © 1998 by Apple Computer, Inc. All rights reserved
Change History (most recent first)
<1> 6/26/98 srk first file
*/
/************************************************************
* *
* INCLUDE FILES *
* *
*************************************************************/
#include "ConditionalMacros.h"
#if TARGET_OS_WIN32
#include <QTML.h>
#endif
#include <MacTypes.h>
#include <MacErrors.h>
#include <Fonts.h>
#include <QuickDraw.h>
#include <FixMath.h>
#include <Sound.h>
#include <Movies.h>
#include <ImageCompression.h>
#include <NumberFormatting.h>
#include "CreateMovie.h"
#include "QTVideo.h"
/************************************************************
* *
* FUNCTION PROTOTYPES *
* *
*************************************************************/
static void QTVideo_AddVideoSamplesToMedia (Media theMedia, const Rect *trackFrame);
static void QTVideo_DrawFrame (const Rect *trackFrame, long curSample);
/************************************************************
* *
* CONSTANTS *
* *
*************************************************************/
#define kVideoTimeScale 600
#define kNumVideoFrames 70
#define kPixelDepth 8 /* use 8-bit depth */
#define kNoOffset 0
#define kMgrChoose 0
#define kSyncSample 0
#define kAddOneVideoSample 1
#define kSampleDuration 60 /* frame duration = 1/10 sec */
#define kTrackStart 0
#define kMediaStart 0
/************************************************************
* *
* QTVideo_CreateMyVideoTrack() *
* *
* Creates a video track for a given QuickTime movie *
* *
*************************************************************/
void QTVideo_CreateMyVideoTrack(Movie theMovie)
{
Track theTrack;
Media theMedia;
OSErr err = noErr;
Rect trackFrame = {0,0,100,320};
theTrack = NewMovieTrack (theMovie,
FixRatio(trackFrame.right,1),
FixRatio(trackFrame.bottom,1),
kNoVolume);
CheckError( GetMoviesError(), "NewMovieTrack error" );
theMedia = NewTrackMedia (theTrack, VideoMediaType,
kVideoTimeScale, /* Video Time Scale */
nil, 0);
CheckError( GetMoviesError(), "NewTrackMedia error" );
err = BeginMediaEdits (theMedia);
CheckError( err, "BeginMediaEdits error" );
QTVideo_AddVideoSamplesToMedia (theMedia, &trackFrame);
err = EndMediaEdits (theMedia);
CheckError( err, "EndMediaEdits error" );
err = InsertMediaIntoTrack (theTrack, kTrackStart,/* track start time */
kMediaStart, /* media start time */
GetMediaDuration (theMedia),
fixed1);
CheckError( err, "InsertMediaIntoTrack error" );
}
/************************************************************
* *
* QTVideo_AddVideoSamplesToMedia() *
* *
* Creates video samples for the media in a track *
* *
*************************************************************/
static void QTVideo_AddVideoSamplesToMedia (Media theMedia, const Rect *trackFrame)
{
long maxCompressedSize;
GWorldPtr theGWorld = nil;
long curSample;
Handle compressedData = nil;
Ptr compressedDataPtr;
ImageDescriptionHandle imageDesc = nil;
CGrafPtr oldPort;
GDHandle oldGDeviceH;
OSErr err = noErr;
err = NewGWorld (&theGWorld,
kPixelDepth, /* pixel depth */
trackFrame,
nil,
nil,
(GWorldFlags) 0 );
CheckError (err, "NewGWorld error");
LockPixels (theGWorld->portPixMap);
err = GetMaxCompressionSize(theGWorld->portPixMap,
trackFrame,
kMgrChoose, /* let ICM choose depth */
codecNormalQuality,
kAnimationCodecType,
(CompressorComponent) anyCodec,
&maxCompressedSize);
CheckError (err, "GetMaxCompressionSize error" );
compressedData = NewHandle(maxCompressedSize);
CheckError( MemError(), "NewHandle error" );
MoveHHi( compressedData );
HLock( compressedData );
compressedDataPtr = StripAddress( *compressedData );
imageDesc = (ImageDescriptionHandle)NewHandle(4);
CheckError( MemError(), "NewHandle error" );
GetGWorld (&oldPort, &oldGDeviceH);
SetGWorld (theGWorld, nil);
for (curSample = 1; curSample <= kNumVideoFrames; curSample++)
{
EraseRect (trackFrame);
QTVideo_DrawFrame(trackFrame, curSample);
err = CompressImage (theGWorld->portPixMap,
trackFrame,
codecNormalQuality,
kAnimationCodecType,
imageDesc,
compressedDataPtr );
CheckError( err, "CompressImage error" );
err = AddMediaSample(theMedia,
compressedData,
kNoOffset, /* no offset in data */
(**imageDesc).dataSize,
kSampleDuration, /* frame duration = 1/10 sec */
(SampleDescriptionHandle)imageDesc,
kAddOneVideoSample, /* one sample */
kSyncSample, /* self-contained samples */
nil);
CheckError( err, "AddMediaSample error" );
}
SetGWorld (oldPort, oldGDeviceH);
if (imageDesc)
{
DisposeHandle ((Handle)imageDesc);
}
if (compressedData)
{
DisposeHandle (compressedData);
}
if (theGWorld)
{
DisposeGWorld (theGWorld);
}
}
/************************************************************
* *
* QTVideo_DrawFrame() *
* *
* contains code to "draw" a video frame *
* *
*************************************************************/
static void QTVideo_DrawFrame (const Rect *trackFrame, long curSample)
{
Str255 numStr;
ForeColor( redColor );
PaintRect( trackFrame );
ForeColor( blueColor );
NumToString (curSample, numStr);
MoveTo ( (short)(trackFrame->right / 2), (short)(trackFrame->bottom / 2));
TextSize ( (short)(trackFrame->bottom / 3));
DrawString (numStr);
}