summaryrefslogtreecommitdiff
path: root/MovieDecode/SimpleTest.c
blob: 6c0139594b85d4329a5403d5ecd51fd6723fd648 (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
/*
**	$Id: SimpleTest.c,v 1.1 93/05/22 13:42:56 chris Exp $
**	$Revision: 1.1 $
**
**	$Filename: MovieDecode/SimpleTest.c $
**	$Author: chris $
**	$Date: 93/05/22 13:42:56 $
**
**	Simple program to test the movie decoder.
**	Usage: SimpleTest <filename>
*/

// #define VERBOSE

#include <proto/exec.h>
#include <exec/memory.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <string.h>

#include <libraries/iff.h>			/* Not an official CBM file */

#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))

#define ID_ANSQ IFFL_MAKE_ID('A','N','S','Q')

#define MAXFRAMES 1024		/* Max # of frames for animation (test) */

struct Library *IFFBase;


/****************************************************************************
**	Prototypes for the MovieDecode functions
*/

APTR __asm
InitJDecoder(register __d0 ULONG animtype);

VOID __asm
FreeJDecoder(register __a0 APTR handle);

BOOL __asm
ModifyJFrame(register __a0 APTR deltas,
             register __a1 struct BitMap *bm,
             register __a2 APTR handle);


#ifdef VERBOSE
/****************************************************************************
**	Not used. Dump a MOVIE frame in ASCII. Ugly code :)
*/

LONG DumpFrame(ULONG *chunk,struct BitMap *bm)
{
	static WORD	frame = 0;
	int			ok = FALSE;
	UWORD		*ptr = (WORD *)(chunk+2);

	Printf("\nFrame %ld\n", ++frame);

	for(;;)
	{
		WORD bytes, rows, offset;
		bytes=*ptr++; ptr++; rows=*ptr++;

		if (bytes==1)
		{
			goto onebyte;
		}
		else if (bytes==2)
		{
			int entries, i;

			bytes = *ptr++;

			if ((bytes!=2) && (bytes!=4) && (bytes!=6))
			{
				Printf("Error: Secondary byte count = %ld\n\n", bytes);
				break;
			}
onebyte:
			if ((rows<1) || ((rows>8) && (rows!=16) && (rows!=32)))
			{
				Printf("Illegal # of rows: %ld\n", rows);
				break;
			}

			entries = *ptr++;
			Printf("  %ld bytes x %ld rows: %ld times\n", bytes, rows, entries);

			for (i=0; i<entries; ++i)
			{
				offset = *ptr++;
				Printf("    %ld: (%ld,%ld)\n",
					offset, offset % bm->BytesPerRow, offset / bm->BytesPerRow);

				ptr = (void *)(((ULONG)ptr) + bm->Depth * rows * bytes);

				if ((bytes==1) && (bm->Depth & 1) && (rows & 1))
					ptr = (void *)(((ULONG)ptr) + 1);
			}
		}
		else if(bytes == 0)
		{
			PutStr("End of frame\n");
			ok = TRUE;
			break;
		}
		else
		{
			Printf("Error: Primary byte count = %ld\n\n", bytes);
			break;
		}
	}
	return ok;
}
#endif


/****************************************************************************
**	Generate the next animation frame
*/

LONG MyModifyFrame(ULONG *form, struct BitMap *bm, APTR decoderhandle)
{
	ULONG *chunk;

	if (chunk = IFFL_FindChunk(form, ID_ANHD))
	{
		int op = ((struct IFFL_ANHD *)(chunk+2))->Operation;

		if (op=='J')
		{
			if (chunk = IFFL_FindChunk(form, ID_DLTA))
			{
#ifdef VERBOSE
				return DumpFrame(chunk, bm);
#else
				return ModifyJFrame(chunk+2, bm, decoderhandle);
#endif
			}
		}
		else return (LONG)IFFL_ModifyFrame(form, bm);	/* ANIM5... */
	}
}


/****************************************************************************
**	Load & display an animation
*/

void ShowAnim(char *name)
{
	struct IFFL_BMHD	*bmhd;
	struct BitMap		*bm1 = NULL, *bm2 = NULL;
	struct Screen		*screen = NULL;
	ULONG				*form;
	int					numframes = 0, i, swidth, sheight;
	WORD				*ansq, *anptr;
	ULONG				*seq[MAXFRAMES];
	APTR				decoderhandle = NULL;
	ULONG				*ifffile;
	BOOL				eor;


	/*
	**	Load animation to memory
	*/
	if (!(ifffile = IFFL_OpenIFF(name, IFFL_MODE_READ)))
	{
		PutStr("Not an IFF file!\n");
		goto cleanup;
	}

	if(*(((ULONG *)ifffile)+2) != ID_ANIM)
	{
		PutStr("Not an ANIM file!\n");
		goto cleanup;
	}

	form = ifffile+3; /* Skip FORM....ANIM */

	if (!(bmhd = IFFL_GetBMHD(form)))
	{
		PutStr("No BMHD\n");
		goto cleanup;
	}
	Printf("%ld x %ld x %ld ", bmhd->w, bmhd->h, bmhd->nPlanes);


	/*
	**	Calculate the screen size. Movie needs at least a 320 x 200
	**	(or 640 x 400 in hires/interlace) screen.
	*/
	swidth = bmhd->w;

	if (swidth < 320)
		swidth = 320;

	if ((swidth > 400) && (swidth < 640))
		swidth = 640;

	sheight = bmhd->h;
	if (sheight < 200)
		sheight = 200;

	if ((sheight > 300) && (sheight < 400))
		sheight = 400;

	Printf("Screen size is %ld x %ld\n", swidth, sheight);


	/*
	**	Allocate the two bitmaps
	*/
	if (!(bm1 = AllocBitMap(swidth, sheight, bmhd->nPlanes, 0/*BMF_DISPLAYABLE*/, NULL)))
	{
		PutStr("Can't allocate bitmap 1!\n");
		goto cleanup;
	}

	if (!(bm2 = AllocBitMap(swidth, sheight, bmhd->nPlanes, 0/*BMF_DISPLAYABLE*/, NULL)))
	{
		PutStr("Can't allocate bitmap 2!\n");
		goto cleanup;
	}


	/*
	**	Open the screen and set the colors
	*/
	if (screen = OpenScreenTags(NULL,
			SA_Width, 		swidth,
			SA_Height,		sheight,
			SA_Depth,		bmhd->nPlanes,
			SA_DisplayID,	IFFL_GetViewModes(form),
			SA_BitMap,		bm1,
			SA_Quiet,		TRUE,
			SA_Behind,		TRUE,
			TAG_DONE
	))
	{
		UWORD	colortab[256];
		int		colorcount = IFFL_GetColorTab(form, (WORD *)colortab);

		LoadRGB4(&screen->ViewPort, colortab, colorcount);
	}
	else
	{
		PutStr("Can't open screen!");
		goto cleanup;
	}


	/*
	**	Get pointers to all delta FORMs
	*/
	while (form = IFFL_FindChunk(form, 0L))
	{
		UWORD *dlta;

		if (*form != ID_FORM) break;

		if (dlta = IFFL_FindChunk(form, ID_DLTA))
		{
			if ((dlta[4]==1) || (dlta[4]==2))
				eor=dlta[5];
		}
		else PutStr("No delta!");
		seq[numframes++] = form;

		if (numframes >= MAXFRAMES-1)
		{
			PutStr("- Too many frames!");
			goto cleanup;
		}
	}


	/*
	**	Determine the compression type
	*/
	if (form = IFFL_FindChunk(ifffile, ID_ANSQ))
	{
		/*
		**	Initialize the decoder
		*/
		if (decoderhandle = InitJDecoder((eor?0x10000L:0) | bm1->Depth))
		{
			Printf(eor ? "Movie reversible, " : "Movie oneway, ");
			anptr = ansq = (WORD *)(form+2);
		}
		else
		{
			PutStr("Can't init decoder!\n");
			goto cleanup;
		}
	}
	else
	{
		Printf("ANIM format, ");
		ansq = NULL;
	}
	Printf("%ld frames.\n", numframes);


	/*
	**	Display the first frame
	*/
	if (!IFFL_DecodePic(ifffile+3, bm1))
	{
		Printf("Decode error: %ld\n", IFFL_IFFError());
		goto cleanup;
	}


	if(bmhd->w < 320)
		ScrollRaster(&screen->RastPort, (bmhd->w-320) / 2, 0, 0, 0, 319, bmhd->h-1);


	/*
	**	Display the second frame: copy and modify the first one
	*/
	BltBitMap(bm1, 0, 0, bm2, 0, 0, screen->Width, screen->Height, 0xc0, 0xff, NULL);
	WaitBlit();

	if (!MyModifyFrame(seq[0], bm2, decoderhandle))
	{
		PutStr("Can't modify 1st frame\n");
		goto cleanup;
	}

	ScreenToFront(screen);


	/*
	**	The loop: Play animation 3 times
	*/
	for(i=0; i<3; ++i)
	{
		int frame = 1;
		for(;;)
		{
			struct BitMap *dummy;

			if (CheckSignal(SIGBREAKF_CTRL_C))
				goto cleanup;

			/*
			**	Flip BitMaps
			*/
			dummy=bm1; bm1=bm2; bm2=dummy;
			screen->BitMap = *bm1;			/* ugly but only a test anyway */
			MakeScreen(screen);
			RethinkDisplay();

#if 01
			if (ansq)
			{
				if ((anptr[1]>0) && (anptr[1]<200))
					Delay(anptr[1]);			/* Wait 'anptr[1]' blanks */
			}
#endif

			if (!MyModifyFrame(seq[ansq?anptr[0]:frame], bm2, decoderhandle))
			{
				PutStr("Can't decode frame\n");
				goto cleanup;
			}

			if (ansq)
			{
				anptr+=2;
				if (anptr[1] == -1)
				{
					anptr = ansq+2*(anptr[0]-1);
					break;
				}
			}
			else
			{
				if (++frame >= numframes)
					break;
			}
		}
	}
	Delay(50);	/* Show the last frame for 1 second */

cleanup:
	PutStr("Cleanup...\n");

	if (decoderhandle)
		FreeJDecoder(decoderhandle);

	if (screen)
		CloseScreen(screen);

	if (bm2)
		FreeBitMap(bm2);

	if (bm1)
		FreeBitMap(bm1);
}


/****************************************************************************
**	Entry point
*/

int main(int argc, char **argv)
{
	if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
		return RETURN_FAIL;

	if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 37)))
		return RETURN_FAIL;

	if (argc == 2)
	{
		if (IFFBase = OpenLibrary(IFFNAME, 19))
		{
			ShowAnim(argv[1]);		/* Show the animation */

			CloseLibrary(IFFBase);
		}
		else PutStr("No iff.library!\n");
	}
	else PutStr("Usage: SimpleTest <filename>\n");

	CloseLibrary((struct Library *)IntuitionBase);
	CloseLibrary((struct Library *)GfxBase);

	return RETURN_OK;
}