varray_* rewritten to remove padding (varray_vertex3f, varray_texcoord2f, varray_texc...
[divverent/darkplaces.git] / cl_video.c
1
2 #include "quakedef.h"
3 #include "cl_video.h"
4 #include "dpvsimpledecode.h"
5
6 mempool_t *clvideomempool;
7
8 int cl_videoplaying = false;
9 void *cl_videostream;
10
11 double cl_videostarttime;
12 int cl_videoframenum;
13 double cl_videoframerate;
14
15 int cl_videoimagewidth;
16 int cl_videoimageheight;
17 int cl_videoimagedata_rmask;
18 int cl_videoimagedata_gmask;
19 int cl_videoimagedata_bmask;
20 int cl_videoimagedata_bytesperpixel;
21 void *cl_videoimagedata;
22
23 int cl_videosoundrate;
24 int cl_videosoundlength;
25 short *cl_videosounddata;
26 int cl_videosoundresamplelength;
27 short *cl_videosoundresampledata;
28
29 rtexture_t *cl_videotexture;
30 rtexturepool_t *cl_videotexturepool;
31
32 void CL_VideoFrame(void)
33 {
34         int frames, framenum, samples, s;
35         if (!cl_videoplaying)
36                 return;
37         framenum = (realtime - cl_videostarttime) * cl_videoframerate;
38         //Con_Printf("frame %i\n", framenum);
39         if (framenum < 0)
40                 framenum = 0;
41         frames = 0;
42         while (cl_videoframenum < framenum)
43         {
44                 frames++;
45                 cl_videoframenum++;
46                 if (dpvsimpledecode_video(cl_videostream, cl_videoimagedata, cl_videoimagedata_rmask, cl_videoimagedata_gmask, cl_videoimagedata_bmask, cl_videoimagedata_bytesperpixel, cl_videoimagewidth * cl_videoimagedata_bytesperpixel))
47                 {
48                         CL_VideoStop();
49                         return;
50                 }
51         }
52         if (frames)
53         {
54                 R_UpdateTexture(cl_videotexture, cl_videoimagedata);
55                 //Draw_NewPic("engine_videoframe", cl_videoimagewidth, cl_videoimageheight, false, cl_videoimagedata);
56         }
57         if (cl_videosoundrate && (samples = S_RawSamples_QueueWantsMore()))
58         {
59                 Con_DPrintf("%i = S_RawSamples_QueueWantsMore()\n", samples);
60
61                 // calculate how much source data we need to fill the output...
62                 s = samples * cl_videosoundrate / S_RawSamples_SampleRate();
63
64                 // reallocate processing buffer if needed
65                 if (cl_videosoundresamplelength < samples)
66                 {
67                         cl_videosoundresamplelength = samples + 100;
68                         if (cl_videosoundresampledata)
69                                 Mem_Free(cl_videosoundresampledata);
70                         cl_videosoundresampledata = Mem_Alloc(clvideomempool, cl_videosoundresamplelength * sizeof(short[2]));
71                 }
72
73                 // reallocate loading buffer if needed
74                 if (cl_videosoundlength < s)
75                 {
76                         cl_videosoundlength = s + 100;
77                         if (cl_videosounddata)
78                                 Mem_Free(cl_videosounddata);
79                         cl_videosounddata = Mem_Alloc(clvideomempool, cl_videosoundlength * sizeof(short[2]));
80                 }
81
82                 dpvsimpledecode_audio(cl_videostream, cl_videosounddata, s);
83                 S_ResampleBuffer16Stereo(cl_videosounddata, s, cl_videosoundresampledata, samples);
84                 S_RawSamples_Enqueue(cl_videosoundresampledata, samples);
85         }
86 }
87
88 void CL_DrawVideo(void)
89 {
90         if (cl_videoplaying)
91         {
92                 drawqueuemesh_t mesh;
93                 float vertex3f[12];
94                 float texcoord2f[8];
95                 float color4f[16];
96                 float s1, t1, s2, t2, x1, y1, x2, y2;
97                 x1 = 0;
98                 y1 = 0;
99                 x2 = vid.conwidth;
100                 y2 = vid.conheight;
101                 R_FragmentLocation(cl_videotexture, NULL, NULL, &s1, &t1, &s2, &t2);
102                 texcoord2f[0] = s1;texcoord2f[1] = t1;
103                 texcoord2f[2] = s2;texcoord2f[3] = t1;
104                 texcoord2f[4] = s2;texcoord2f[5] = t2;
105                 texcoord2f[6] = s1;texcoord2f[7] = t2;
106                 R_FillColors(color4f, 4, r_colorscale, r_colorscale, r_colorscale, 1);
107                 vertex3f[ 0] = x1;vertex3f[ 1] = y1;vertex3f[ 2] = 0;
108                 vertex3f[ 3] = x2;vertex3f[ 4] = y1;vertex3f[ 5] = 0;
109                 vertex3f[ 6] = x2;vertex3f[ 7] = y2;vertex3f[ 8] = 0;
110                 vertex3f[ 9] = x1;vertex3f[10] = y2;vertex3f[11] = 0;
111                 mesh.texture = cl_videotexture;
112                 mesh.numtriangles = 2;
113                 mesh.numvertices = 4;
114                 mesh.element3i = polygonelements;
115                 mesh.vertex3f = vertex3f;
116                 mesh.texcoord2f = texcoord2f;
117                 mesh.color4f = color4f;
118                 DrawQ_Mesh(&mesh, 0);
119                 //DrawQ_Pic(0, 0, "engine_videoframe", vid.conwidth, vid.conheight, 1, 1, 1, 1, 0);
120         }
121 }
122
123 void CL_VideoStart(char *filename)
124 {
125         char *errorstring;
126         cl_videostream = dpvsimpledecode_open(filename, &errorstring);
127         if (!cl_videostream)
128         {
129                 Con_Printf("unable to open \"%s\", error: %s\n", filename, errorstring);
130                 return;
131         }
132
133         cl_videoplaying = true;
134         cl_videostarttime = realtime;
135         cl_videoframenum = -1;
136         cl_videoframerate = dpvsimpledecode_getframerate(cl_videostream);
137         cl_videoimagewidth = dpvsimpledecode_getwidth(cl_videostream);
138         cl_videoimageheight = dpvsimpledecode_getheight(cl_videostream);
139
140         // RGBA format
141         cl_videoimagedata_bytesperpixel = 4;
142         cl_videoimagedata_rmask = BigLong(0xFF000000);
143         cl_videoimagedata_gmask = BigLong(0x00FF0000);
144         cl_videoimagedata_bmask = BigLong(0x0000FF00);
145         cl_videoimagedata = Mem_Alloc(clvideomempool, cl_videoimagewidth * cl_videoimageheight * cl_videoimagedata_bytesperpixel);
146         //memset(cl_videoimagedata, 97, cl_videoimagewidth * cl_videoimageheight * cl_videoimagedata_bytesperpixel);
147
148         cl_videosoundrate = dpvsimpledecode_getsoundrate(cl_videostream);
149         cl_videosoundlength = 0;
150         cl_videosounddata = NULL;
151         cl_videosoundresamplelength = 0;
152         cl_videosoundresampledata = NULL;
153
154         cl_videotexturepool = R_AllocTexturePool();
155         cl_videotexture = R_LoadTexture2D(cl_videotexturepool, "videotexture", cl_videoimagewidth, cl_videoimageheight, NULL, TEXTYPE_RGBA, TEXF_FRAGMENT, NULL);
156 }
157
158 void CL_VideoStop(void)
159 {
160         cl_videoplaying = false;
161
162         S_RawSamples_ClearQueue();
163
164         if (cl_videostream)
165                 dpvsimpledecode_close(cl_videostream);
166         cl_videostream = NULL;
167
168         if (cl_videoimagedata)
169                 Mem_Free(cl_videoimagedata);
170         cl_videoimagedata = NULL;
171
172         if (cl_videosounddata)
173                 Mem_Free(cl_videosounddata);
174         cl_videosounddata = NULL;
175
176         if (cl_videosoundresampledata)
177                 Mem_Free(cl_videosoundresampledata);
178         cl_videosoundresampledata = NULL;
179
180         cl_videotexture = NULL;
181         R_FreeTexturePool(&cl_videotexturepool);
182
183         Draw_FreePic("engine_videoframe");
184 }
185
186 static void CL_PlayVideo_f(void)
187 {
188         char name[1024];
189
190         if (Cmd_Argc() != 2)
191         {
192                 Con_Printf ("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
193                 return;
194         }
195
196         sprintf(name, "%s/video/%s.dpv", com_gamedir, Cmd_Argv(1));
197         CL_VideoStart(name);
198 }
199
200 static void CL_StopVideo_f(void)
201 {
202         CL_VideoStop();
203 }
204
205 void CL_Video_Init(void)
206 {
207         Cmd_AddCommand("playvideo", CL_PlayVideo_f);
208         Cmd_AddCommand("stopvideo", CL_StopVideo_f);
209
210         clvideomempool = Mem_AllocPool("CL_Video");
211 }