1 // JAM format decoder, used by Blood Omnicide
3 typedef struct jamdecodestream_s
9 unsigned int info_frames;
10 unsigned int info_imagewidth;
11 unsigned int info_imageheight;
14 unsigned char colorsub;
17 // info used durign decoding
18 unsigned char *videopixels;
19 unsigned char *compressed;
20 unsigned char *framedata;
21 unsigned char *prevframedata;
22 unsigned char colormap[768];
23 unsigned int framesize;
24 unsigned int framenum;
26 // channel the sound file is being played on
31 #define JAMDECODEERROR_NONE 0
32 #define JAMDECODEERROR_EOF 1
33 #define JAMDECODEERROR_READERROR 2
34 #define JAMDECODEERROR_BAD_FRAME_HEADER 3
35 #define JAMDECODEERROR_BAD_OUTPUT_SIZE 4
36 #define JAMDECODEERROR_BAD_COLORMAP 5
39 void jam_close(void *stream);
40 unsigned int jam_getwidth(void *stream);
41 unsigned int jam_getheight(void *stream);
42 double jam_getframerate(void *stream);
43 int jam_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow);
44 static void *jam_open(clvideo_t *video, char *filename, const char **errorstring)
46 unsigned char jamHead[16];
51 s = (jamdecodestream_t *)Z_Malloc(sizeof(jamdecodestream_t));
54 if ((file = FS_OpenVirtualFile(filename, false)))
57 if (FS_Read(s->file, &jamHead, 16))
59 if (!memcmp(jamHead, "JAM", 3))
61 s->info_imagewidth = LittleLong(*(jamHead + 4));
62 s->info_imageheight = LittleLong(*(jamHead + 8));
63 s->info_frames = LittleLong(*(jamHead + 12));
64 s->info_framerate = 15;
69 s->framesize = s->info_imagewidth * s->info_imageheight;
72 s->compressed = (unsigned char *)Z_Malloc(s->framesize);
73 s->framedata = (unsigned char *)Z_Malloc(s->framesize * 2);
74 s->prevframedata = (unsigned char *)Z_Malloc(s->framesize * 2);
75 s->videopixels = (unsigned char *)Z_Malloc(s->framesize * 4); // bgra, doubleres
76 if (s->compressed != NULL && s->framedata != NULL && s->prevframedata != NULL && s->videopixels != NULL)
80 namelen = strlen(filename) + 10;
81 wavename = (char *)Z_Malloc(namelen);
86 FS_StripExtension(filename, wavename, namelen);
87 strlcat(wavename, ".wav", namelen);
88 sfx = S_PrecacheSound(wavename, false, false);
90 s->sndchan = S_StartSound (-1, 0, sfx, vec3_origin, 1.0f, 0);
96 // set the module functions
98 video->close = jam_close;
99 video->getwidth = jam_getwidth;
100 video->getheight = jam_getheight;
101 video->getframerate = jam_getframerate;
102 video->decodeframe = jam_video;
105 else if (errorstring != NULL)
106 *errorstring = "unable to allocate memory for stream info structure";
107 if (s->compressed != NULL)
108 Z_Free(s->compressed);
109 if (s->framedata != NULL)
110 Z_Free(s->framedata);
111 if (s->prevframedata != NULL)
112 Z_Free(s->prevframedata);
113 if (s->videopixels != NULL)
114 Z_Free(s->videopixels);
116 else if (errorstring != NULL)
117 *errorstring = "bad framesize";
119 else if (errorstring != NULL)
120 *errorstring = "not JAM videofile";
122 else if (errorstring != NULL)
123 *errorstring = "unexpected EOF";
126 else if (errorstring != NULL)
127 *errorstring = "unable to open videofile";
130 else if (errorstring != NULL)
131 *errorstring = "unable to allocate memory for stream info structure";
136 void jam_close(void *stream)
138 jamdecodestream_t *s = (jamdecodestream_t *)stream;
141 Z_Free(s->compressed);
142 Z_Free(s->framedata);
143 Z_Free(s->prevframedata);
144 Z_Free(s->videopixels);
145 if (s->sndchan != -1)
146 S_StopChannel(s->sndchan, true, true);
152 // returns the width of the image data
153 unsigned int jam_getwidth(void *stream)
155 jamdecodestream_t *s = (jamdecodestream_t *)stream;
157 return s->info_imagewidth * 2;
158 return s->info_imagewidth;
161 // returns the height of the image data
162 unsigned int jam_getheight(void *stream)
164 jamdecodestream_t *s = (jamdecodestream_t *)stream;
166 return s->info_imageheight * 2;
167 return s->info_imageheight;
170 // returns the framerate of the stream
171 double jam_getframerate(void *stream)
173 jamdecodestream_t *s = (jamdecodestream_t *)stream;
174 return s->info_framerate;
179 static void jam_decodeframe(unsigned char *inbuf, unsigned char *outbuf, unsigned char *prevbuf, int outsize, int frametype)
181 unsigned char *srcptr, *destptr, *prevptr;
184 unsigned short int bits;
197 memcpy(outbuf, inbuf, outsize);
202 memcpy(&mark, srcptr, 4);
204 for(i=0; i<32 && bytesleft > 0; i++,mark=mark>>1)
216 bits = srcptr[0] + 256*srcptr[1];
217 rep = (bits >> 11) + 3;
220 backoffs = 0x821 - (bits & 0x7ff);
221 back = destptr - backoffs;
225 backoffs = 0x400 - (bits & 0x7ff);
226 back = prevptr - backoffs;
229 memcpy(destptr, back, rep);
238 // decodes a video frame to the supplied output pixels
239 int jam_video(void *stream, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow)
241 unsigned char frameHead[16], *b;
242 unsigned int compsize, outsize, i, j;
243 jamdecodestream_t *s = (jamdecodestream_t *)stream;
245 s->error = DPVSIMPLEDECODEERROR_NONE;
246 if (s->framenum < s->info_frames)
249 if (FS_Read(s->file, &frameHead, 16))
251 compsize = LittleLong(*(frameHead + 8)) - 16;
252 outsize = LittleLong(*(frameHead + 12));
253 if (compsize > s->framesize || outsize > s->framesize)
254 s->error = JAMDECODEERROR_BAD_FRAME_HEADER;
255 else if (FS_Read(s->file, s->compressed, compsize))
257 // palette goes interleaved with special flag
258 if (frameHead[0] == 2)
262 memcpy(s->colormap, s->compressed, 768);
263 for(i = 0; i < 768; i++)
264 s->colormap[i] = (unsigned char)(bound(0, (s->colormap[i] * s->colorscale) - s->colorsub, 255));
268 // s->error = JAMDECODEERROR_BAD_COLORMAP;
273 // shift buffers to provide current and previous one, decode
274 b = s->prevframedata;
275 s->prevframedata = s->framedata;
277 jam_decodeframe(s->compressed, s->framedata, s->prevframedata, outsize, frameHead[4]);
278 // make 32bit imagepixels from 8bit palettized frame
282 b = (unsigned char *)imagedata;
283 for(i = 0; i < s->framesize; i++)
286 *b++ = s->colormap[s->framedata[i]*3 + 2];
287 *b++ = s->colormap[s->framedata[i]*3 + 1];
288 *b++ = s->colormap[s->framedata[i]*3];
294 for (i = 0; i < s->info_imageheight; i++)
296 b = (unsigned char *)imagedata + (s->info_imagewidth*2*4)*(i*2);
297 for (j = 0; j < s->info_imagewidth; j++)
299 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4];
300 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 1];
301 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 2];
302 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 3];
304 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4];
305 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 1];
306 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 2];
307 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 3];
309 b = (unsigned char *)imagedata + (s->info_imagewidth*2*4)*(i*2 + 1);
310 for (j = 0; j < s->info_imagewidth; j++)
312 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4];
313 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 1];
314 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 2];
315 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 3];
317 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4];
318 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 1];
319 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 2];
320 *b++ = s->videopixels[i*s->info_imagewidth*4 + j*4 + 3];
326 for (i = 0; i < s->info_imageheight; i++)
328 b = (unsigned char *)imagedata + (s->info_imagewidth * 4 * 2 * 2 * i);
329 for (j = 0; j < s->info_imagewidth; j++)
331 b[0] = b[0] * s->stipple;
332 b[1] = b[1] * s->stipple;
333 b[2] = b[2] * s->stipple;
335 b[0] = b[0] * s->stipple;
336 b[1] = b[1] * s->stipple;
337 b[2] = b[2] * s->stipple;
347 s->error = JAMDECODEERROR_READERROR;
350 s->error = JAMDECODEERROR_READERROR;
353 s->error = DPVSIMPLEDECODEERROR_EOF;