Skip to content

Commit a08f25f

Browse files
committed
Initial revision
git-svn-id: svn://svn.icculus.org/twilight/trunk/dpvideo@2070 d7cf8633-e32d-0410-b094-e92efae38249
0 parents  commit a08f25f

36 files changed

+11029
-0
lines changed

dpvdecode.c

+944
Large diffs are not rendered by default.

dpvdecode.h

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
#ifndef DPVDECODE_H
3+
#define DPVDECODE_H
4+
5+
#define DPVDECODEERROR_NONE 0
6+
#define DPVDECODEERROR_EOF 1
7+
#define DPVDECODEERROR_READERROR 2
8+
#define DPVDECODEERROR_SOUNDBUFFERTOOSMALL 3
9+
#define DPVDECODEERROR_INVALIDRMASK 4
10+
#define DPVDECODEERROR_INVALIDGMASK 5
11+
#define DPVDECODEERROR_INVALIDBMASK 6
12+
#define DPVDECODEERROR_COLORMASKSOVERLAP 7
13+
#define DPVDECODEERROR_COLORMASKSEXCEEDBPP 8
14+
#define DPVDECODEERROR_UNSUPPORTEDBPP 9
15+
16+
// opening and closing streams
17+
18+
// opens a stream
19+
void *dpvdecode_open(char *filename, char **errorstring);
20+
// closes a stream
21+
void dpvdecode_close(void *stream);
22+
23+
// utilitarian functions
24+
25+
// returns the current error number for the stream, and resets the error
26+
// number to DPVDECODEERROR_NONE
27+
// if the supplied string pointer variable is not NULL, it will be set to the
28+
// error message
29+
int dpvdecode_error(void *stream, char **errorstring);
30+
31+
// retrieve frame number for given time
32+
int dpvdecode_framefortime(void *stream, double t);
33+
34+
// return the total number of frames in the stream
35+
unsigned int dpvdecode_gettotalframes(void *stream);
36+
37+
// return the total time of the stream
38+
double dpvdecode_gettotaltime(void *stream);
39+
40+
// returns the width of the image data
41+
unsigned int dpvdecode_getwidth(void *stream);
42+
43+
// returns the height of the image data
44+
unsigned int dpvdecode_getheight(void *stream);
45+
46+
// returns the sound sample rate of the stream
47+
unsigned int dpvdecode_getsoundrate(void *stream);
48+
49+
// returns the framerate of the stream
50+
double dpvdecode_getframerate(void *stream);
51+
52+
// returns a recommended sound buffer length (in samples)
53+
// for decoding a single frame of this stream
54+
unsigned int dpvdecode_getneededsoundbufferlength(void *stream);
55+
56+
// decodes a frame, both video and audio, to the supplied buffers
57+
// can produce many different possible errors
58+
// (such as too little space in supplied sound buffer)
59+
// (note: sound is 16bit stereo native-endian, left channel first)
60+
//int dpvdecode_frame(void *stream, int framenum, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow, short *sounddata, unsigned int soundbufferlength, unsigned int *soundlength);
61+
62+
// decodes a video frame to the supplied output pixels
63+
int dpvdecode_video(void *stream, int framenum, void *imagedata, unsigned int Rmask, unsigned int Gmask, unsigned int Bmask, unsigned int bytesperpixel, int imagebytesperrow);
64+
// reads some sound
65+
// (note: sound is 16bit stereo native-endian, left channel first)
66+
int dpvdecode_audio(void *stream, int firstsample, short *soundbuffer, int requestedlength);
67+
68+
#endif

dpvdecoder.c

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <stdarg.h>
5+
#include <string.h>
6+
#include "dpvdecode.h"
7+
#include "tgafile.h"
8+
9+
void Error (char *message, ...)
10+
{
11+
va_list argptr;
12+
13+
puts("ERROR: ");
14+
15+
va_start (argptr,message);
16+
vprintf (message,argptr);
17+
va_end (argptr);
18+
putchar('\n');
19+
#if _DEBUG && WIN32
20+
printf ("press a key\n");
21+
getchar();
22+
#endif
23+
exit (1);
24+
}
25+
26+
void StripExtension(char *in, char *out)
27+
{
28+
char *dot;
29+
dot = strrchr(in, '.');
30+
if (dot)
31+
{
32+
if (dot < strrchr(in, '/'))
33+
dot = NULL;
34+
if (dot < strrchr(in, '\\'))
35+
dot = NULL;
36+
if (dot < strrchr(in, ':'))
37+
dot = NULL;
38+
}
39+
if (dot == NULL)
40+
dot = in + strlen(in);
41+
while (in < dot)
42+
*out++ = *in++;
43+
*out++ = 0;
44+
}
45+
46+
47+
void usage(void)
48+
{
49+
printf(
50+
"usage: dpvdecoder <name>\n"
51+
"example:\n"
52+
"dpvdecoder test\n"
53+
"would save out all the frames of test.dpv as test00000.tga, test00001.tga\n"
54+
"and so on, and save out the sound as test.wav\n"
55+
);
56+
}
57+
58+
int main(int argc, char **argv)
59+
{
60+
int errornum;
61+
int width, height/*, soundbufferlength, soundlength*/, framenum;
62+
void *imagedata;
63+
//short *sounddata;
64+
char *errormessage;
65+
void *stream;
66+
char *basename;
67+
char filename[1024];
68+
char framename[1024];
69+
char wavname[1024];
70+
if (argc != 2)
71+
{
72+
usage();
73+
return 1;
74+
}
75+
basename = argv[1];
76+
77+
sprintf(filename, "%s.dpv", basename);
78+
stream = dpvdecode_open(filename, &errormessage);
79+
if (stream == NULL)
80+
{
81+
printf("unable to open stream file \"%s\", file does not exist or is not a valid stream\ndpvdecode_error reported: %s\n", filename, errormessage);
82+
83+
strcpy(filename, basename);
84+
StripExtension(basename, basename);
85+
stream = dpvdecode_open(filename, &errormessage);
86+
if (stream == NULL)
87+
{
88+
printf("unable to open stream file \"%s\", file does not exist or is not a valid stream\ndpvdecode_error reported: %s\n", filename, errormessage);
89+
return 1;
90+
}
91+
}
92+
93+
sprintf(wavname, "%s.wav", basename);
94+
// FIXME: write wav save code
95+
errornum = 0;
96+
width = dpvdecode_getwidth(stream);
97+
height = dpvdecode_getheight(stream);
98+
//soundbufferlength = dpvdecode_getneededsoundbufferlength(stream);
99+
imagedata = malloc(width * height * 4);
100+
//sounddata = malloc(soundlength * sizeof(short[2]));
101+
for (framenum = 0;;framenum++)
102+
{
103+
//dpvdecode_frame(stream, framenum, imagedata, 0xFF0000, 0x00FF00, 0x0000FF, 4, width * 4, sounddata, soundbufferlength, &soundlength);
104+
dpvdecode_video(stream, framenum, imagedata, 0xFF0000, 0x00FF00, 0x0000FF, 4, width * 4);
105+
if (dpvdecode_error(stream, &errormessage))
106+
break;
107+
sprintf(framename, "%s%04d.tga", basename, framenum);
108+
savetga_rgb32_topdown(framename, imagedata, width, height);
109+
}
110+
if (errornum)
111+
printf("error while decoding stream: %s\n", errormessage);
112+
dpvdecode_close(stream);
113+
return 0;
114+
}

0 commit comments

Comments
 (0)