-
Notifications
You must be signed in to change notification settings - Fork 12
/
dreamroqlib.c
904 lines (792 loc) · 29.3 KB
/
dreamroqlib.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
* Dreamroq by Mike Melanson
* Audio support by Josh Pearson
*
* This is the main playback engine.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dreamroqlib.h"
#define RoQ_INFO 0x1001
#define RoQ_QUAD_CODEBOOK 0x1002
#define RoQ_QUAD_VQ 0x1011
#define RoQ_JPEG 0x1012
#define RoQ_SOUND_MONO 0x1020
#define RoQ_SOUND_STEREO 0x1021
#define RoQ_PACKET 0x1030
#define RoQ_SIGNATURE 0x1084
#define CHUNK_HEADER_SIZE 8
#define LE_16(buf) (*buf | (*(buf+1) << 8))
#define LE_32(buf) (*buf | (*(buf+1) << 8) | (*(buf+2) << 16) | (*(buf+3) << 24))
#define MAX_BUF_SIZE (64 * 1024)
#define ROQ_CODEBOOK_SIZE 256
#define SQR_ARRAY_SIZE 256
struct roq_audio
{
int pcm_samples;
int channels;
int position;
short snd_sqr_array[SQR_ARRAY_SIZE];
unsigned char pcm_sample[MAX_BUF_SIZE];
} roq_audio;
typedef struct
{
int width;
int height;
int mb_width;
int mb_height;
int mb_count;
int alpha;
int current_frame;
unsigned char *frame[2];
int stride;
int texture_height;
int colorspace;
unsigned short cb2x2_rgb565[ROQ_CODEBOOK_SIZE][4];
unsigned short cb4x4_rgb565[ROQ_CODEBOOK_SIZE][16];
unsigned int cb2x2_rgba[ROQ_CODEBOOK_SIZE][4];
unsigned int cb4x4_rgba[ROQ_CODEBOOK_SIZE][16];
} roq_state;
static int roq_unpack_quad_codebook_rgb565(unsigned char *buf, int size,
int arg, roq_state *state)
{
int y[4];
int yp, u, v;
int r, g, b;
int count2x2;
int count4x4;
int i, j;
unsigned short *v2x2;
unsigned short *v4x4;
count2x2 = (arg >> 8) & 0xFF;
count4x4 = arg & 0xFF;
if (!count2x2)
count2x2 = ROQ_CODEBOOK_SIZE;
/* 0x00 means 256 4x4 vectors iff there is enough space in the chunk
* after accounting for the 2x2 vectors */
if (!count4x4 && count2x2 * 6 < size)
count4x4 = ROQ_CODEBOOK_SIZE;
/* size sanity check, taking alpha into account */
if (state->alpha && (count2x2 * 10 + count4x4 * 4) != size)
{
return ROQ_BAD_CODEBOOK;
}
if (!state->alpha && (count2x2 * 6 + count4x4 * 4) != size)
{
return ROQ_BAD_CODEBOOK;
}
/* unpack the 2x2 vectors */
for (i = 0; i < count2x2; i++)
{
/* unpack the YUV components from the bytestream */
for (j = 0; j < 4; j++)
{
y[j] = *buf++;
if (state->alpha)
buf++;
}
u = *buf++;
v = *buf++;
/* convert to RGB565 */
for (j = 0; j < 4; j++)
{
yp = (y[j] - 16) * 1.164;
r = (yp + 1.596 * (v - 128)) / 8;
g = (yp - 0.813 * (v - 128) - 0.391 * (u - 128)) / 4;
b = (yp + 2.018 * (u - 128)) / 8;
if (r < 0) r = 0;
if (r > 31) r = 31;
if (g < 0) g = 0;
if (g > 63) g = 63;
if (b < 0) b = 0;
if (b > 31) b = 31;
state->cb2x2_rgb565[i][j] = (
(r << 11) |
(g << 5) |
(b << 0) );
}
}
/* unpack the 4x4 vectors */
for (i = 0; i < count4x4; i++)
{
for (j = 0; j < 4; j++)
{
v2x2 = state->cb2x2_rgb565[*buf++];
v4x4 = state->cb4x4_rgb565[i] + (j / 2) * 8 + (j % 2) * 2;
v4x4[0] = v2x2[0];
v4x4[1] = v2x2[1];
v4x4[4] = v2x2[2];
v4x4[5] = v2x2[3];
}
}
return ROQ_SUCCESS;
}
static int roq_unpack_quad_codebook_rgba(unsigned char *buf, int size,
int arg, roq_state *state)
{
int y[4];
int a[4];
int yp, u, v;
int r, g, b;
int count2x2;
int count4x4;
int i, j;
unsigned int *v2x2;
unsigned int *v4x4;
count2x2 = (arg >> 8) & 0xFF;
count4x4 = arg & 0xFF;
if (!count2x2)
count2x2 = ROQ_CODEBOOK_SIZE;
/* 0x00 means 256 4x4 vectors iff there is enough space in the chunk
* after accounting for the 2x2 vectors */
if (!count4x4 && count2x2 * 6 < size)
count4x4 = ROQ_CODEBOOK_SIZE;
/* size sanity check, taking alpha into account */
if (state->alpha && (count2x2 * 10 + count4x4 * 4) != size)
{
return ROQ_BAD_CODEBOOK;
}
if (!state->alpha && (count2x2 * 6 + count4x4 * 4) != size)
{
return ROQ_BAD_CODEBOOK;
}
/* unpack the 2x2 vectors */
for (i = 0; i < count2x2; i++)
{
/* unpack the YUV components from the bytestream */
for (j = 0; j < 4; j++)
{
y[j] = *buf++;
if (state->alpha)
a[j] = *buf++;
else
a[j] = 255;
}
u = *buf++;
v = *buf++;
/* convert to RGBA */
for (j = 0; j < 4; j++)
{
yp = (y[j] - 16) * 1.164;
r = (yp + 1.596 * (v - 128));
g = (yp - 0.813 * (v - 128) - 0.391 * (u - 128));
b = (yp + 2.018 * (u - 128));
if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
if (b < 0) b = 0;
if (b > 255) b = 255;
state->cb2x2_rgba[i][j] = (
(r << 24) |
(g << 16) |
(b << 8) |
a[j]);
}
}
/* unpack the 4x4 vectors */
for (i = 0; i < count4x4; i++)
{
for (j = 0; j < 4; j++)
{
v2x2 = state->cb2x2_rgba[*buf++];
v4x4 = state->cb4x4_rgba[i] + (j / 2) * 8 + (j % 2) * 2;
v4x4[0] = v2x2[0];
v4x4[1] = v2x2[1];
v4x4[4] = v2x2[2];
v4x4[5] = v2x2[3];
}
}
return ROQ_SUCCESS;
}
#define GET_BYTE(x) \
if (index >= size) { \
status = ROQ_BAD_VQ_STREAM; \
x = 0; \
} else { \
x = buf[index++]; \
}
#define GET_MODE() \
if (!mode_count) { \
GET_BYTE(mode_lo); \
GET_BYTE(mode_hi); \
mode_set = (mode_hi << 8) | mode_lo; \
mode_count = 16; \
} \
mode_count -= 2; \
mode = (mode_set >> mode_count) & 0x03;
static int roq_unpack_vq_rgb565(unsigned char *buf, int size, unsigned int arg,
roq_state *state)
{
int status = ROQ_SUCCESS;
int mb_x, mb_y;
int block; /* 8x8 blocks */
int subblock; /* 4x4 blocks */
int stride = state->stride;
int i;
/* frame and pixel management */
unsigned short *this_frame;
unsigned short *last_frame;
int line_offset;
int mb_offset;
int block_offset;
int subblock_offset;
unsigned short *this_ptr;
unsigned int *this_ptr32;
unsigned short *last_ptr;
unsigned short *vector16;
unsigned int *vector32;
int stride32 = stride / 2;
/* bytestream management */
int index = 0;
int mode_set = 0;
int mode, mode_lo, mode_hi;
int mode_count = 0;
/* vectors */
int mx, my;
int motion_x, motion_y;
unsigned char data_byte;
mx = (signed char)(arg >> 8);
my = (signed char)arg;
if (state->current_frame & 1)
{
this_frame = (unsigned short*)state->frame[1];
last_frame = (unsigned short*)state->frame[0];
}
else
{
this_frame = (unsigned short*)state->frame[0];
last_frame = (unsigned short*)state->frame[1];
}
/* special case for frame 1, which needs to begin with frame 0's data */
if (state->current_frame == 1)
{
memcpy(state->frame[1], state->frame[0],
state->texture_height * state->stride * sizeof(unsigned short));
}
for (mb_y = 0; mb_y < state->mb_height && status == ROQ_SUCCESS; mb_y++)
{
line_offset = mb_y * 16 * stride;
for (mb_x = 0; mb_x < state->mb_width && status == ROQ_SUCCESS; mb_x++)
{
mb_offset = line_offset + mb_x * 16;
for (block = 0; block < 4 && status == ROQ_SUCCESS; block++)
{
block_offset = mb_offset + (block / 2 * 8 * stride) + (block % 2 * 8);
/* each 8x8 block gets a mode */
GET_MODE();
switch (mode)
{
case 0: /* MOT: skip */
break;
case 1: /* FCC: motion compensation */
/* this needs to be done 16 bits at a time due to
* data alignment issues on the SH-4 */
GET_BYTE(data_byte);
motion_x = 8 - (data_byte >> 4) - mx;
motion_y = 8 - (data_byte & 0xF) - my;
last_ptr = last_frame + block_offset +
(motion_y * stride) + motion_x;
this_ptr = this_frame + block_offset;
for (i = 0; i < 8; i++)
{
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
last_ptr += stride - 8;
this_ptr += stride - 8;
}
break;
case 2: /* SLD: upsample 4x4 vector */
GET_BYTE(data_byte);
vector16 = state->cb4x4_rgb565[data_byte];
for (i = 0; i < 4*4; i++)
{
this_ptr = this_frame + block_offset +
(i / 4 * 2 * stride) + (i % 4 * 2);
this_ptr[0] = *vector16;
this_ptr[1] = *vector16;
this_ptr[stride+0] = *vector16;
this_ptr[stride+1] = *vector16;
vector16++;
}
break;
case 3: /* CCC: subdivide into 4 subblocks */
for (subblock = 0; subblock < 4; subblock++)
{
subblock_offset = block_offset + (subblock / 2 * 4 * stride) + (subblock % 2 * 4);
GET_MODE();
switch (mode)
{
case 0: /* MOT: skip */
break;
case 1: /* FCC: motion compensation */
GET_BYTE(data_byte);
motion_x = 8 - (data_byte >> 4) - mx;
motion_y = 8 - (data_byte & 0xF) - my;
last_ptr = last_frame + subblock_offset +
(motion_y * stride) + motion_x;
this_ptr = this_frame + subblock_offset;
for (i = 0; i < 4; i++)
{
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
last_ptr += stride - 4;
this_ptr += stride - 4;
}
break;
case 2: /* SLD: use 4x4 vector from codebook */
GET_BYTE(data_byte);
vector32 = (unsigned int*)state->cb4x4_rgb565[data_byte];
this_ptr32 = (unsigned int*)this_frame;
this_ptr32 += subblock_offset / 2;
for (i = 0; i < 4; i++)
{
*this_ptr32++ = *vector32++;
*this_ptr32++ = *vector32++;
this_ptr32 += stride32 - 2;
}
break;
case 3: /* CCC: subdivide into 4 subblocks */
GET_BYTE(data_byte);
vector16 = state->cb2x2_rgb565[data_byte];
this_ptr = this_frame + subblock_offset;
this_ptr[0] = vector16[0];
this_ptr[1] = vector16[1];
this_ptr[stride+0] = vector16[2];
this_ptr[stride+1] = vector16[3];
GET_BYTE(data_byte);
vector16 = state->cb2x2_rgb565[data_byte];
this_ptr[2] = vector16[0];
this_ptr[3] = vector16[1];
this_ptr[stride+2] = vector16[2];
this_ptr[stride+3] = vector16[3];
this_ptr += stride * 2;
GET_BYTE(data_byte);
vector16 = state->cb2x2_rgb565[data_byte];
this_ptr[0] = vector16[0];
this_ptr[1] = vector16[1];
this_ptr[stride+0] = vector16[2];
this_ptr[stride+1] = vector16[3];
GET_BYTE(data_byte);
vector16 = state->cb2x2_rgb565[data_byte];
this_ptr[2] = vector16[0];
this_ptr[3] = vector16[1];
this_ptr[stride+2] = vector16[2];
this_ptr[stride+3] = vector16[3];
break;
}
}
break;
}
}
}
}
/* sanity check to see if the stream was fully consumed */
if (status == ROQ_SUCCESS && index < size-2)
{
status = ROQ_BAD_VQ_STREAM;
}
return status;
}
static int roq_unpack_vq_rgba(unsigned char *buf, int size, unsigned int arg,
roq_state *state)
{
int status = ROQ_SUCCESS;
int mb_x, mb_y;
int block; /* 8x8 blocks */
int subblock; /* 4x4 blocks */
int stride = state->stride;
int i;
/* frame and pixel management */
unsigned int *this_frame;
unsigned int *last_frame;
int line_offset;
int mb_offset;
int block_offset;
int subblock_offset;
unsigned int *this_ptr;
unsigned int *last_ptr;
unsigned int *vector;
/* bytestream management */
int index = 0;
int mode_set = 0;
int mode, mode_lo, mode_hi;
int mode_count = 0;
/* vectors */
int mx, my;
int motion_x, motion_y;
unsigned char data_byte;
mx = (signed char)(arg >> 8);
my = (signed char)arg;
if (state->current_frame & 1)
{
this_frame = (unsigned int*)state->frame[1];
last_frame = (unsigned int*)state->frame[0];
}
else
{
this_frame = (unsigned int*)state->frame[0];
last_frame = (unsigned int*)state->frame[1];
}
/* special case for frame 1, which needs to begin with frame 0's data */
if (state->current_frame == 1)
{
memcpy(state->frame[1], state->frame[0],
state->texture_height * state->stride * sizeof(unsigned int));
}
for (mb_y = 0; mb_y < state->mb_height && status == ROQ_SUCCESS; mb_y++)
{
line_offset = mb_y * 16 * stride;
for (mb_x = 0; mb_x < state->mb_width && status == ROQ_SUCCESS; mb_x++)
{
mb_offset = line_offset + mb_x * 16;
for (block = 0; block < 4 && status == ROQ_SUCCESS; block++)
{
block_offset = mb_offset + (block / 2 * 8 * stride) + (block % 2 * 8);
/* each 8x8 block gets a mode */
GET_MODE();
switch (mode)
{
case 0: /* MOT: skip */
break;
case 1: /* FCC: motion compensation */
GET_BYTE(data_byte);
motion_x = 8 - (data_byte >> 4) - mx;
motion_y = 8 - (data_byte & 0xF) - my;
last_ptr = last_frame + block_offset +
(motion_y * stride) + motion_x;
this_ptr = this_frame + block_offset;
for (i = 0; i < 8; i++)
{
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
last_ptr += stride - 8;
this_ptr += stride - 8;
}
break;
case 2: /* SLD: upsample 4x4 vector */
GET_BYTE(data_byte);
vector = state->cb4x4_rgba[data_byte];
for (i = 0; i < 4*4; i++)
{
this_ptr = this_frame + block_offset +
(i / 4 * 2 * stride) + (i % 4 * 2);
this_ptr[0] = *vector;
this_ptr[1] = *vector;
this_ptr[stride+0] = *vector;
this_ptr[stride+1] = *vector;
vector++;
}
break;
case 3: /* CCC: subdivide into 4 subblocks */
for (subblock = 0; subblock < 4; subblock++)
{
subblock_offset = block_offset + (subblock / 2 * 4 * stride) + (subblock % 2 * 4);
GET_MODE();
switch (mode)
{
case 0: /* MOT: skip */
break;
case 1: /* FCC: motion compensation */
GET_BYTE(data_byte);
motion_x = 8 - (data_byte >> 4) - mx;
motion_y = 8 - (data_byte & 0xF) - my;
last_ptr = last_frame + subblock_offset +
(motion_y * stride) + motion_x;
this_ptr = this_frame + subblock_offset;
for (i = 0; i < 4; i++)
{
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
*this_ptr++ = *last_ptr++;
last_ptr += stride - 4;
this_ptr += stride - 4;
}
break;
case 2: /* SLD: use 4x4 vector from codebook */
GET_BYTE(data_byte);
vector = state->cb4x4_rgba[data_byte];
this_ptr = this_frame + subblock_offset;
for (i = 0; i < 4; i++)
{
*this_ptr++ = *vector++;
*this_ptr++ = *vector++;
*this_ptr++ = *vector++;
*this_ptr++ = *vector++;
this_ptr += stride - 4;
}
break;
case 3: /* CCC: subdivide into 4 subblocks */
GET_BYTE(data_byte);
vector = state->cb2x2_rgba[data_byte];
this_ptr = this_frame + subblock_offset;
this_ptr[0] = vector[0];
this_ptr[1] = vector[1];
this_ptr[stride+0] = vector[2];
this_ptr[stride+1] = vector[3];
GET_BYTE(data_byte);
vector = state->cb2x2_rgba[data_byte];
this_ptr[2] = vector[0];
this_ptr[3] = vector[1];
this_ptr[stride+2] = vector[2];
this_ptr[stride+3] = vector[3];
this_ptr += stride * 2;
GET_BYTE(data_byte);
vector = state->cb2x2_rgba[data_byte];
this_ptr[0] = vector[0];
this_ptr[1] = vector[1];
this_ptr[stride+0] = vector[2];
this_ptr[stride+1] = vector[3];
GET_BYTE(data_byte);
vector = state->cb2x2_rgba[data_byte];
this_ptr[2] = vector[0];
this_ptr[3] = vector[1];
this_ptr[stride+2] = vector[2];
this_ptr[stride+3] = vector[3];
break;
}
}
break;
}
}
}
}
/* sanity check to see if the stream was fully consumed */
if (status == ROQ_SUCCESS && index < size-2)
{
status = ROQ_BAD_VQ_STREAM;
}
return status;
}
int dreamroq_play(char *filename, int colorspace, int loop,
roq_callbacks_t *cbs)
{
FILE *f;
size_t file_ret;
int framerate;
int chunk_id;
unsigned int chunk_size;
unsigned int chunk_arg;
roq_state state;
int status;
int initialized = 0;
unsigned char read_buffer[MAX_BUF_SIZE];
int i, snd_left, snd_right;
f = fopen(filename, "rb");
if (!f)
return ROQ_FILE_OPEN_FAILURE;
file_ret = fread(read_buffer, CHUNK_HEADER_SIZE, 1, f);
if (file_ret != 1)
{
fclose(f);
printf("\nROQ_FILE_READ_FAILURE\n\n");
return ROQ_FILE_READ_FAILURE;
}
chunk_id = LE_16(&read_buffer[0]);
chunk_size = LE_32(&read_buffer[2]);
if (chunk_id != RoQ_SIGNATURE || chunk_size != 0xFFFFFFFF)
{
fclose(f);
return ROQ_FILE_READ_FAILURE;
}
framerate = LE_16(&read_buffer[6]);
printf("RoQ file plays at %d frames/sec\n", framerate);
/* Initialize Audio SQRT Look-Up Table */
for(i = 0; i < 128; i++)
{
roq_audio.snd_sqr_array[i] = i * i;
roq_audio.snd_sqr_array[i + 128] = -(i * i);
}
status = ROQ_SUCCESS;
while (!feof(f) && status == ROQ_SUCCESS)
{
/* if client program defined a quit callback, check if it's time
* to quit */
if (cbs->quit_cb && cbs->quit_cb())
break;
file_ret = fread(read_buffer, CHUNK_HEADER_SIZE, 1, f);
if (file_ret != 1)
{
/* if the read failed but the file is not EOF, there is a deeper
* problem; don't entertain the idea of looping */
if (!feof(f))
break;
else if (loop)
{
/* it shouldn't be necessary to close and re-open the file
* here; however, this works around a bug in KOS 1.2.0 in
* which seeking back doesn't clear the EOF flag */
fclose(f);
f = fopen(filename, "rb");
if (!f)
status = ROQ_FILE_OPEN_FAILURE;
else
{
/* skip the signature header */
fseek(f, 8, SEEK_SET);
continue;
}
}
else
break;
}
chunk_id = LE_16(&read_buffer[0]);
chunk_size = LE_32(&read_buffer[2]);
chunk_arg = LE_16(&read_buffer[6]);
if (chunk_size > MAX_BUF_SIZE)
{
fclose(f);
return ROQ_CHUNK_TOO_LARGE;
}
file_ret = fread(read_buffer, chunk_size, 1, f);
if (file_ret != 1)
{
status = ROQ_FILE_READ_FAILURE;
break;
}
state.colorspace = colorspace;
switch(chunk_id)
{
case RoQ_INFO:
if (initialized)
continue;
state.alpha = chunk_arg;
state.width = LE_16(&read_buffer[0]);
state.height = LE_16(&read_buffer[2]);
/* width and height each need to be divisible by 16 */
if ((state.width & 0xF) || (state.height & 0xF))
{
status = ROQ_INVALID_PIC_SIZE;
break;
}
state.mb_width = state.width / 16;
state.mb_height = state.height / 16;
state.mb_count = state.mb_width * state.mb_height;
if (state.width < 8 || state.width > 1024)
status = ROQ_INVALID_DIMENSION;
else
{
state.stride = 8;
while (state.stride < state.width)
state.stride <<= 1;
}
if (state.height < 8 || state.height > 1024)
status = ROQ_INVALID_DIMENSION;
else
{
state.texture_height = 8;
while (state.texture_height < state.height)
state.texture_height <<= 1;
}
printf(" RoQ_INFO: dimensions = %dx%d, %dx%d; %d mbs, texture = %dx%d\n",
state.width, state.height, state.mb_width, state.mb_height,
state.mb_count, state.stride, state.texture_height);
if (colorspace == ROQ_RGB565)
{
state.frame[0] = (unsigned char*)malloc(state.texture_height * state.stride * sizeof(unsigned short));
state.frame[1] = (unsigned char*)malloc(state.texture_height * state.stride * sizeof(unsigned short));
}
else
{
state.frame[0] = (unsigned char*)malloc(state.texture_height * state.stride * sizeof(unsigned int));
state.frame[1] = (unsigned char*)malloc(state.texture_height * state.stride * sizeof(unsigned int));
}
state.current_frame = 0;
if (!state.frame[0] || !state.frame[1])
{
free (state.frame[0]);
free (state.frame[1]);
status = ROQ_NO_MEMORY;
break;
}
memset(state.frame[0], 0, state.texture_height * state.stride * sizeof(unsigned short));
memset(state.frame[1], 0, state.texture_height * state.stride * sizeof(unsigned short));
/* set this flag so that this code is not executed again when
* looping */
initialized = 1;
break;
case RoQ_QUAD_CODEBOOK:
if (colorspace == ROQ_RGB565)
status = roq_unpack_quad_codebook_rgb565(read_buffer, chunk_size,
chunk_arg, &state);
else if (colorspace == ROQ_RGBA)
status = roq_unpack_quad_codebook_rgba(read_buffer, chunk_size,
chunk_arg, &state);
break;
case RoQ_QUAD_VQ:
if (colorspace == ROQ_RGB565)
status = roq_unpack_vq_rgb565(read_buffer, chunk_size,
chunk_arg, &state);
else if (colorspace == ROQ_RGBA)
status = roq_unpack_vq_rgba(read_buffer, chunk_size,
chunk_arg, &state);
if (cbs->render_cb)
status = cbs->render_cb(state.frame[state.current_frame & 1],
state.width, state.height, state.stride, state.texture_height,
colorspace);
state.current_frame++;
break;
case RoQ_JPEG:
break;
case RoQ_SOUND_MONO:
roq_audio.channels = 1;
roq_audio.pcm_samples = chunk_size*2;
snd_left = chunk_arg;
for(i = 0; i < chunk_size; i++)
{
snd_left += roq_audio.snd_sqr_array[read_buffer[i]];
roq_audio.pcm_sample[i * 2] = snd_left & 0xff;
roq_audio.pcm_sample[i * 2 + 1] = (snd_left & 0xff00) >> 8;
}
if (cbs->audio_cb)
status = cbs->audio_cb(roq_audio.pcm_sample, roq_audio.pcm_samples,
roq_audio.channels);
break;
case RoQ_SOUND_STEREO:
roq_audio.channels = 2;
roq_audio.pcm_samples = chunk_size*2;
snd_left = (chunk_arg & 0xFF00);
snd_right = (chunk_arg & 0xFF) << 8;
for(i = 0; i < chunk_size; i += 2)
{
snd_left += roq_audio.snd_sqr_array[read_buffer[i]];
snd_right += roq_audio.snd_sqr_array[read_buffer[i+1]];
roq_audio.pcm_sample[i * 2] = snd_left & 0xff;
roq_audio.pcm_sample[i * 2 + 1] = (snd_left & 0xff00) >> 8;
roq_audio.pcm_sample[i * 2 + 2] = snd_right & 0xff;
roq_audio.pcm_sample[i * 2 + 3] = (snd_right & 0xff00) >> 8;
}
if (cbs->audio_cb)
status = cbs->audio_cb( roq_audio.pcm_sample, roq_audio.pcm_samples,
roq_audio.channels );
break;
case RoQ_PACKET:
/* still unimplemented */
break;
default:
break;
}
}
free(state.frame[0]);
free(state.frame[1]);
fclose(f);
if (cbs->finish_cb)
cbs->finish_cb();
return status;
}