-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcinepak.c
400 lines (317 loc) · 8.24 KB
/
cinepak.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
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
/* :ts=8 bk=0
*
* cinepak.c: CinePak support code.
*
* Leo L. Schwab 9309.28
*/
#define DEBUG 1
#include <types.h>
#include <graphics.h>
#include "DataStreamDebug.h"
#include "DataStreamLib.h"
#include "DataAcq.h"
#include "CPakSubscriber.h"
#include "SAudioSubscriber.h"
#include "ControlSubscriber.h"
#include "castle.h"
#include "app_proto.h"
/***************************************************************************
* #defines
*/
#define NSTREAMS 1
#define STREAMER_DELTA_PRI 6
#define SUBSCRIBER_DELTA_PRI 7
#define AUDIO_SUBSCRIBER_DELTA_PRI 10
#define CTRL_SUBSCRIBER_DELTA_PRI (AUDIO_SUBSCRIBER_DELTA_PRI + 1)
#define DATA_ACQ_DELTA_PRI 8
#define DATA_BUFFER_COUNT 5
#define DATA_BUFFER_SIZE (128 * 1024)
#define ROUND_TO_LONG(x) ((x + 3) & ~3)
/***************************************************************************
* Local prototypes.
*/
static DSDataBuf *createbufferlist (long, long);
/***************************************************************************
* Globaloids.
*/
extern RastPort *rpvis, *rprend;
extern JoyData jd;
extern Item vblIO;
static CtrlContext *sc_ctl;
static SAudioContext *sc_saudio;
static CPakContext *sc_cpak;
static CPakRec *cpakrec;
static DSStreamCB *streamcb;
static DSDataBuf *streambuf;
static AcqContext *acqctxt;
static Item streamport;
static Item streammsg;
/***************************************************************************
* Code. (As if you needed this comment to tell you that. Duh...)
*/
void
initstreaming ()
{
/*
* Create communication items.
*/
if ((streamport = NewMsgPort (NULL)) < 0)
die ("Can't create streamport.\n");
if ((streammsg = CreateMsgItem (streamport)) < 0)
die ("Can't create streammsg.\n");
/*
* Initialize required streaming modules.
*/
if (InitDataAcq (1) < 0 ||
InitDataStreaming (NSTREAMS) < 0)
die ("Stream system initialization failed.\n");
if (InitCPakSubscriber () < 0 ||
InitSAudioSubscriber () < 0 ||
InitCtrlSubscriber () < 0)
die ("Subscriber module initialization failed.\n");
/*
* Create buffer list.
*/
if (NewDataStream (&streamcb, NULL, DATA_BUFFER_SIZE,
STREAMER_DELTA_PRI) < 0)
die ("NewDataStream() failed.\n");
/*
* Initialize Control subscriber.
*/
if (NewCtrlSubscriber (&sc_ctl,
streamcb,
CTRL_SUBSCRIBER_DELTA_PRI) < 0)
die ("NewCtrlSubscriber() failed.\n");
if (DSSubscribe (streammsg, NULL, streamcb,
CTRL_CHUNK_TYPE,
sc_ctl->requestPort) < 0)
die ("Control subscription failed.\n");
/*
* Initialize audio subscriber.
*/
if (NewSAudioSubscriber (&sc_saudio,
streamcb,
AUDIO_SUBSCRIBER_DELTA_PRI) < 0)
die ("NewSAudioSubscriber() failed.\n");
if (DSSubscribe (streammsg, NULL, streamcb,
SNDS_CHUNK_TYPE,
sc_saudio->requestPort) < 0)
die ("Audio subscription failed.\n");
/*
* Initialize CinePak subscriber.
*/
if (NewCPakSubscriber (&sc_cpak, NSTREAMS, SUBSCRIBER_DELTA_PRI) < 0)
die ("NewCPakSubscriber() failed.\n");
if (DSSubscribe (streammsg, NULL, streamcb,
FILM_CHUNK_TYPE,
sc_cpak->requestPort) < 0)
die ("CinePak subscription failed.\n");
if (InitCPakCel (streamcb,
sc_cpak,
&cpakrec,
0,
TRUE) < 0)
die ("InitCPakCel() failed.\n");
}
void
shutdownstreaming ()
{
DSSubscribe (streammsg, NULL, streamcb, FILM_CHUNK_TYPE, 0);
DSSubscribe (streammsg, NULL, streamcb, SNDS_CHUNK_TYPE, 0);
DSSubscribe (streammsg, NULL, streamcb, CTRL_CHUNK_TYPE, 0);
DestroyCPakCel (sc_cpak, cpakrec, 0);
DisposeCtrlSubscriber (sc_ctl);
DisposeSAudioSubscriber (sc_saudio);
DisposeCPakSubscriber (sc_cpak);
DisposeDataStream (streammsg, streamcb); streamcb = NULL;
RemoveMsgItem (streammsg); streammsg = 0;
RemoveMsgPort (streamport); streamport = 0;
}
/***************************************************************************
* Data stream buffer management.
*/
static DSDataBuf *
createbufferlist (
long numBuffers,
long bufferSize
)
{
DSDataBuf *bp;
DSDataBuf *lastBp;
long totalBufferSpace;
long actualEntrySize;
actualEntrySize = sizeof (DSDataBuf) + ROUND_TO_LONG (bufferSize);
totalBufferSpace = numBuffers * actualEntrySize;
if (!(bp = malloc (totalBufferSpace)))
die ("malloc() of buffer list space failed!\n");
/* Make a linked list of entries */
lastBp = NULL;
while (--numBuffers >= 0)
{
bp->permanentNext = lastBp;
bp->next = lastBp;
lastBp = bp;
bp = (DSDataBuf *) (((char *) bp) + actualEntrySize);
}
/*
* Return a pointer to the first buffer in the list.
*/
return (lastBp);
}
void
freebufferlist (buf)
struct DSDataBuf *buf;
{
if (buf) {
while (buf->permanentNext)
buf = buf->permanentNext;
free (buf);
}
}
/***************************************************************************
* Individual stream management.
*/
static int32 templateTags[] = {
SA_22K_16B_S_SDX2,
SA_22K_16B_M_SDX2,
0
};
void
openstream (filename)
char *filename;
{
SAudioCtlBlock audiocmd;
/*
* Actually create buffer and install into DSStreamCB.
* NOTE: This am a hack; may not be supported. (Works, though.)
*/
streambuf = createbufferlist (DATA_BUFFER_COUNT, DATA_BUFFER_SIZE);
streamcb->freeBufHead = streambuf;
/*
* Initialize data acquisition
*/
if (NewDataAcq (&acqctxt, filename, DATA_ACQ_DELTA_PRI) < 0)
die ("Failed to acquire stream file.\n");
if (DSConnect (streammsg, NULL, streamcb,
acqctxt->requestPort) < 0)
die ("Failed to connect stream to acquisitor.\n");
audiocmd.loadTemplates.tagListPtr = templateTags;
if (DSControl (streammsg, NULL, streamcb,
SNDS_CHUNK_TYPE,
kSAudioCtlOpLoadTemplates,
&audiocmd) < 0)
die ("Couldn't load audio templates.\n");
if (DSSetChannel (streammsg, NULL, streamcb,
SNDS_CHUNK_TYPE,
1,
CHAN_ENABLED) < 0)
die ("Couldn't set stream channel.\n");
if (DSGoMarker (streammsg, NULL, streamcb,
0,
GOMARKER_ABSOLUTE) < 0)
die ("Couldn't seek to marker.\n");
if (DSPreRollStream (streammsg, FALSE, streamcb) < 0)
die ("Preroll failed.\n");
}
void
closestream ()
{
SAudioCtlBlock audiocmd;
audiocmd.closeChannel.channelNumber = 1;
if (DSControl (streammsg, NULL, streamcb,
SNDS_CHUNK_TYPE,
kSAudioCtlOpCloseChannel,
&audiocmd) < 0)
die ("Couldn't close audio channel.\n");
if (DSConnect (streammsg, NULL, streamcb, 0) < 0)
die ("Disconnect hack failed.\n");
DisposeDataAcq (acqctxt); acqctxt = NULL;
streamcb->freeBufHead = NULL;
freebufferlist (streambuf); streambuf = NULL;
}
/***************************************************************************
* Convenient goodies.
*/
int
endofstream ()
{
register struct DSDataBuf *buf;
register int usecnt;
if (!(streamcb->streamFlags & STRM_EOF))
return (FALSE);
buf = streambuf;
usecnt = 0;
while (buf) {
usecnt += buf->useCount;
buf = buf->permanentNext;
}
//kprintf ("usecnt was %d\n", usecnt);
return (usecnt < 2);
}
struct CCB *
getstreamccb ()
{
register CCB *ccb;
ccb = GetCPakCel (sc_cpak, cpakrec);
SendFreeCPakSignal (sc_cpak);
return (ccb);
}
void
uncpaktorp (rp)
struct RastPort *rp;
{
DrawCPakToBuffer (sc_cpak, cpakrec, rp->rp_Bitmap);
SendFreeCPakSignal (sc_cpak);
}
void
startstream ()
{
if (DSStartStream (streammsg, NULL, streamcb, 0) < 0)
die ("Failed to start stream.\n");
}
void
stopstream ()
{
if (DSStopStream (streammsg, NULL, streamcb, SOPT_FLUSH) < 0)
die ("Failed to stop stream.\n");
FlushCPakChannel (sc_cpak, cpakrec, 0);
}
/***************************************************************************
* Even more convenient goodie.
*/
int
playcpak (filename)
char *filename;
{
register RastPort *tmp;
int retval, foo;
retval = FALSE;
SetRast (rpvis, 0);
SetRast (rprend, 0);
DisplayScreen (rpvis->rp_ScreenItem, 0);
openstream (filename);
startstream ();
resetjoydata ();
while (!endofstream ()) {
uncpaktorp (rprend);
tmp = rpvis; rpvis = rprend; rprend = tmp;
DisplayScreen (rpvis->rp_ScreenItem, 0);
CopyRast (rpvis, rprend);
// WaitVBL (vblIO, 1);
if (jd.jd_ADown || jd.jd_BDown || jd.jd_CDown ||
jd.jd_DX || jd.jd_XDown || jd.jd_StartDown)
{
retval = TRUE;
break;
}
}
stopstream ();
closestream ();
resetjoydata ();
if (foo = performElKabong (NULL)) {
kprintf ("El Kabong failed.\n");
PrintfSysErr (foo);
}
fadetoblank (rpvis, 30);
return (retval);
}