4 #define AVI_MASTER_INDEX_SIZE 640 // GB ought to be enough for anyone
6 typedef struct capturevideostate_avi_formatspecific_s
9 fs_offset_t videofile_firstchunkframes_offset;
10 fs_offset_t videofile_totalframes_offset1;
11 fs_offset_t videofile_totalframes_offset2;
12 fs_offset_t videofile_totalsampleframes_offset;
13 int videofile_ix_master_audio_inuse;
14 fs_offset_t videofile_ix_master_audio_inuse_offset;
15 fs_offset_t videofile_ix_master_audio_start_offset;
16 int videofile_ix_master_video_inuse;
17 fs_offset_t videofile_ix_master_video_inuse_offset;
18 fs_offset_t videofile_ix_master_video_start_offset;
19 fs_offset_t videofile_ix_movistart;
23 unsigned char riffbufferdata[128];
24 sizebuf_t riffindexbuffer;
26 fs_offset_t riffstackstartoffset[4];
27 fs_offset_t riffstacksizehint[4];
28 const char *riffstackfourcc[4];
30 capturevideostate_avi_formatspecific_t;
31 #define LOAD_FORMATSPECIFIC_AVI() capturevideostate_avi_formatspecific_t *format = (capturevideostate_avi_formatspecific_t *) cls.capturevideo.formatspecific
33 static void SCR_CaptureVideo_RIFF_Start(void)
35 LOAD_FORMATSPECIFIC_AVI();
36 memset(&format->riffbuffer, 0, sizeof(sizebuf_t));
37 format->riffbuffer.maxsize = sizeof(format->riffbufferdata);
38 format->riffbuffer.data = format->riffbufferdata;
42 static void SCR_CaptureVideo_RIFF_Flush(void)
44 LOAD_FORMATSPECIFIC_AVI();
45 if (format->riffbuffer.cursize > 0)
47 if (!FS_Write(cls.capturevideo.videofile, format->riffbuffer.data, format->riffbuffer.cursize))
48 cls.capturevideo.error = true;
49 format->position += format->riffbuffer.cursize;
50 format->riffbuffer.cursize = 0;
51 format->riffbuffer.overflowed = false;
55 static void SCR_CaptureVideo_RIFF_FlushNoIncrease(void)
57 LOAD_FORMATSPECIFIC_AVI();
58 if (format->riffbuffer.cursize > 0)
60 if (!FS_Write(cls.capturevideo.videofile, format->riffbuffer.data, format->riffbuffer.cursize))
61 cls.capturevideo.error = true;
62 format->riffbuffer.cursize = 0;
63 format->riffbuffer.overflowed = false;
67 static void SCR_CaptureVideo_RIFF_WriteBytes(const unsigned char *data, size_t size)
69 LOAD_FORMATSPECIFIC_AVI();
70 SCR_CaptureVideo_RIFF_Flush();
71 if (!FS_Write(cls.capturevideo.videofile, data, size))
72 cls.capturevideo.error = true;
73 format->position += size;
76 static void SCR_CaptureVideo_RIFF_Write32(int n)
78 LOAD_FORMATSPECIFIC_AVI();
79 if (format->riffbuffer.cursize + 4 > format->riffbuffer.maxsize)
80 SCR_CaptureVideo_RIFF_Flush();
81 MSG_WriteLong(&format->riffbuffer, n);
84 static void SCR_CaptureVideo_RIFF_Write16(int n)
86 LOAD_FORMATSPECIFIC_AVI();
87 if (format->riffbuffer.cursize + 2 > format->riffbuffer.maxsize)
88 SCR_CaptureVideo_RIFF_Flush();
89 MSG_WriteShort(&format->riffbuffer, n);
92 static void SCR_CaptureVideo_RIFF_WriteFourCC(const char *chunkfourcc)
94 LOAD_FORMATSPECIFIC_AVI();
95 if (format->riffbuffer.cursize + (int)strlen(chunkfourcc) > format->riffbuffer.maxsize)
96 SCR_CaptureVideo_RIFF_Flush();
97 MSG_WriteUnterminatedString(&format->riffbuffer, chunkfourcc);
100 static void SCR_CaptureVideo_RIFF_WriteTerminatedString(const char *string)
102 LOAD_FORMATSPECIFIC_AVI();
103 if (format->riffbuffer.cursize + (int)strlen(string) > format->riffbuffer.maxsize)
104 SCR_CaptureVideo_RIFF_Flush();
105 MSG_WriteString(&format->riffbuffer, string);
108 static fs_offset_t SCR_CaptureVideo_RIFF_GetPosition(void)
110 LOAD_FORMATSPECIFIC_AVI();
111 SCR_CaptureVideo_RIFF_Flush();
112 //return FS_Tell(cls.capturevideo.videofile);
113 return format->position;
116 static void SCR_CaptureVideo_RIFF_Push(const char *chunkfourcc, const char *listtypefourcc, fs_offset_t sizeHint)
118 LOAD_FORMATSPECIFIC_AVI();
119 if (listtypefourcc && sizeHint >= 0)
120 sizeHint += 4; // size hint is for INNER size
121 SCR_CaptureVideo_RIFF_WriteFourCC(chunkfourcc);
122 SCR_CaptureVideo_RIFF_Write32(sizeHint);
123 SCR_CaptureVideo_RIFF_Flush();
124 format->riffstacksizehint[format->riffstacklevel] = sizeHint;
125 format->riffstackstartoffset[format->riffstacklevel] = SCR_CaptureVideo_RIFF_GetPosition();
126 format->riffstackfourcc[format->riffstacklevel] = chunkfourcc;
127 ++format->riffstacklevel;
129 SCR_CaptureVideo_RIFF_WriteFourCC(listtypefourcc);
132 static void SCR_CaptureVideo_RIFF_Pop(void)
134 LOAD_FORMATSPECIFIC_AVI();
135 fs_offset_t offset, sizehint;
137 unsigned char sizebytes[4];
138 // write out the chunk size and then return to the current file position
139 format->riffstacklevel--;
140 offset = SCR_CaptureVideo_RIFF_GetPosition();
142 sizehint = format->riffstacksizehint[format->riffstacklevel];
143 x = (int)(offset - (format->riffstackstartoffset[format->riffstacklevel]));
150 Con_Warnf("WARNING: invalid size hint %d when writing video data (actual size: %d)\n", (int) sizehint, x);
151 for(i = 0; i <= format->riffstacklevel; ++i)
153 Con_Printf(" RIFF level %d = %s\n", i, format->riffstackfourcc[i]);
156 sizebytes[0] = (x) & 0xff;sizebytes[1] = (x >> 8) & 0xff;sizebytes[2] = (x >> 16) & 0xff;sizebytes[3] = (x >> 24) & 0xff;
157 if(FS_Seek(cls.capturevideo.videofile, -(x + 4), SEEK_END) >= 0)
159 FS_Write(cls.capturevideo.videofile, sizebytes, 4);
161 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
166 SCR_CaptureVideo_RIFF_WriteBytes((unsigned char *) "\0", 1);
170 static void GrowBuf(sizebuf_t *buf, int extralen)
172 if(buf->cursize + extralen > buf->maxsize)
174 int oldsize = buf->maxsize;
175 unsigned char *olddata;
177 buf->maxsize = max(buf->maxsize * 2, 4096);
178 buf->data = (unsigned char *) Mem_Alloc(tempmempool, buf->maxsize);
181 memcpy(buf->data, olddata, oldsize);
187 static void SCR_CaptureVideo_RIFF_IndexEntry(const char *chunkfourcc, int chunksize, int flags)
189 LOAD_FORMATSPECIFIC_AVI();
191 Sys_Error("SCR_CaptureVideo_RIFF_IndexEntry called on non-seekable AVI");
193 if (format->riffstacklevel != 2)
194 Sys_Error("SCR_Capturevideo_RIFF_IndexEntry: RIFF stack level is %i (should be 2)\n", format->riffstacklevel);
195 GrowBuf(&format->riffindexbuffer, 16);
196 SCR_CaptureVideo_RIFF_Flush();
197 MSG_WriteUnterminatedString(&format->riffindexbuffer, chunkfourcc);
198 MSG_WriteLong(&format->riffindexbuffer, flags);
199 MSG_WriteLong(&format->riffindexbuffer, (int)FS_Tell(cls.capturevideo.videofile) - format->riffstackstartoffset[1]);
200 MSG_WriteLong(&format->riffindexbuffer, chunksize);
203 static void SCR_CaptureVideo_RIFF_MakeIxChunk(const char *fcc, const char *dwChunkId, fs_offset_t masteridx_counter, int *masteridx_count, fs_offset_t masteridx_start)
205 LOAD_FORMATSPECIFIC_AVI();
208 fs_offset_t ix = SCR_CaptureVideo_RIFF_GetPosition();
212 Sys_Error("SCR_CaptureVideo_RIFF_MakeIxChunk called on non-seekable AVI");
214 if(*masteridx_count >= AVI_MASTER_INDEX_SIZE)
217 nMatching = 0; // go through index and enumerate them
218 for(i = 0; i < format->riffindexbuffer.cursize; i += 16)
219 if(!memcmp(format->riffindexbuffer.data + i, dwChunkId, 4))
223 for(i = 0; i < format->riffindexbuffer.cursize; i += 16)
224 if(!memcmp(format->riffindexbuffer.data + i, dwChunkId, 4))
227 SCR_CaptureVideo_RIFF_Push(fcc, NULL, sz);
228 SCR_CaptureVideo_RIFF_Write16(2); // wLongsPerEntry
229 SCR_CaptureVideo_RIFF_Write16(0x0100); // bIndexType=1, bIndexSubType=0
230 SCR_CaptureVideo_RIFF_Write32(nMatching); // nEntriesInUse
231 SCR_CaptureVideo_RIFF_WriteFourCC(dwChunkId); // dwChunkId
232 SCR_CaptureVideo_RIFF_Write32(format->videofile_ix_movistart & (fs_offset_t) 0xFFFFFFFFu);
233 SCR_CaptureVideo_RIFF_Write32(((fs_offset_t) format->videofile_ix_movistart) >> 32);
234 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved
236 for(i = 0; i < format->riffindexbuffer.cursize; i += 16)
237 if(!memcmp(format->riffindexbuffer.data + i, dwChunkId, 4))
239 unsigned int *p = (unsigned int *) (format->riffindexbuffer.data + i);
240 unsigned int flags = p[1];
241 unsigned int rpos = p[2];
242 unsigned int size = p[3];
244 if(!(flags & 0x10)) // no keyframe?
246 SCR_CaptureVideo_RIFF_Write32(rpos + 8);
247 SCR_CaptureVideo_RIFF_Write32(size);
250 SCR_CaptureVideo_RIFF_Flush();
251 SCR_CaptureVideo_RIFF_Pop();
252 pos = SCR_CaptureVideo_RIFF_GetPosition();
254 if(FS_Seek(cls.capturevideo.videofile, masteridx_start + 16 * *masteridx_count, SEEK_SET) >= 0)
256 SCR_CaptureVideo_RIFF_Write32(ix & (fs_offset_t) 0xFFFFFFFFu);
257 SCR_CaptureVideo_RIFF_Write32(((fs_offset_t) ix) >> 32);
258 SCR_CaptureVideo_RIFF_Write32(pos - ix);
259 SCR_CaptureVideo_RIFF_Write32(nMatching);
260 SCR_CaptureVideo_RIFF_FlushNoIncrease();
263 if(FS_Seek(cls.capturevideo.videofile, masteridx_counter, SEEK_SET) >= 0)
265 SCR_CaptureVideo_RIFF_Write32(++*masteridx_count);
266 SCR_CaptureVideo_RIFF_FlushNoIncrease();
269 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END); // return value doesn't matter here
272 static void SCR_CaptureVideo_RIFF_Finish(qboolean final)
274 LOAD_FORMATSPECIFIC_AVI();
275 // close the "movi" list
276 SCR_CaptureVideo_RIFF_Pop();
277 if(format->videofile_ix_master_video_inuse_offset)
278 SCR_CaptureVideo_RIFF_MakeIxChunk("ix00", "00dc", format->videofile_ix_master_video_inuse_offset, &format->videofile_ix_master_video_inuse, format->videofile_ix_master_video_start_offset);
279 if(format->videofile_ix_master_audio_inuse_offset)
280 SCR_CaptureVideo_RIFF_MakeIxChunk("ix01", "01wb", format->videofile_ix_master_audio_inuse_offset, &format->videofile_ix_master_audio_inuse, format->videofile_ix_master_audio_start_offset);
281 // write the idx1 chunk that we've been building while saving the frames (for old style players)
282 if(final && format->videofile_firstchunkframes_offset)
283 // TODO replace index creating by OpenDML ix##/##ix/indx chunk so it works for more than one AVI part too
285 SCR_CaptureVideo_RIFF_Push("idx1", NULL, format->riffindexbuffer.cursize);
286 SCR_CaptureVideo_RIFF_WriteBytes(format->riffindexbuffer.data, format->riffindexbuffer.cursize);
287 SCR_CaptureVideo_RIFF_Pop();
289 format->riffindexbuffer.cursize = 0;
290 // pop the RIFF chunk itself
291 while (format->riffstacklevel > 0)
292 SCR_CaptureVideo_RIFF_Pop();
293 SCR_CaptureVideo_RIFF_Flush();
294 if(format->videofile_firstchunkframes_offset)
296 Con_DPrintf("Finishing first chunk (%d frames)\n", cls.capturevideo.frame);
297 if(FS_Seek(cls.capturevideo.videofile, format->videofile_firstchunkframes_offset, SEEK_SET) >= 0)
299 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
300 SCR_CaptureVideo_RIFF_FlushNoIncrease();
302 FS_Seek(cls.capturevideo.videofile, 0, SEEK_END);
303 format->videofile_firstchunkframes_offset = 0;
306 Con_DPrintf("Finishing another chunk (%d frames)\n", cls.capturevideo.frame);
309 static void SCR_CaptureVideo_RIFF_OverflowCheck(int framesize)
311 LOAD_FORMATSPECIFIC_AVI();
313 //fs_offset_t curfilesize;
314 if (format->riffstacklevel != 2)
315 Sys_Error("SCR_CaptureVideo_RIFF_OverflowCheck: chunk stack leakage!\n");
320 // check where we are in the file
321 SCR_CaptureVideo_RIFF_Flush();
322 cursize = SCR_CaptureVideo_RIFF_GetPosition() - format->riffstackstartoffset[0];
323 //curfilesize = SCR_CaptureVideo_RIFF_GetPosition();
325 // if this would overflow the windows limit of 1GB per RIFF chunk, we need
326 // to close the current RIFF chunk and open another for future frames
327 if (8 + cursize + framesize + format->riffindexbuffer.cursize + 8 + format->riffindexbuffer.cursize + 64 > 1<<30) // note that the Ix buffer takes less space... I just don't dare to / 2 here now... sorry, maybe later
329 SCR_CaptureVideo_RIFF_Finish(false);
330 // begin a new 1GB extended section of the AVI
331 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX", -1);
332 SCR_CaptureVideo_RIFF_Push("LIST", "movi", -1);
333 format->videofile_ix_movistart = format->riffstackstartoffset[1];
337 // converts from BGRA32 to I420 colorspace (identical to YV12 except chroma plane order is reversed), this colorspace is handled by the Intel(r) 4:2:0 codec on Windows
338 static void SCR_CaptureVideo_ConvertFrame_BGRA_to_I420_flip(int width, int height, unsigned char *instart, unsigned char *outstart)
341 int blockr, blockg, blockb;
342 int outoffset = (width/2)*(height/2);
343 unsigned char *b, *out;
344 // process one line at a time, and CbCr every other line at 2 pixel intervals
345 for (y = 0;y < height;y++)
348 for (b = instart + (height-1-y)*width*4, out = outstart + y*width, x = 0;x < width;x++, b += 4, out++)
353 *out = cls.capturevideo.yuvnormalizetable[0][cls.capturevideo.rgbtoyuvscaletable[0][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[0][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[0][2][blockb]];
355 if ((y & 1) == 0 && y/2 < height/2) // if h is odd, this skips the last row
357 // 2x2 Cr and Cb planes
358 int inpitch = width*4;
359 for (b = instart + (height-2-y)*width*4, out = outstart + width*height + (y/2)*(width/2), x = 0;x < width/2;x++, b += 8, out++)
361 blockr = (b[2] + b[6] + b[inpitch+2] + b[inpitch+6]) >> 2;
362 blockg = (b[1] + b[5] + b[inpitch+1] + b[inpitch+5]) >> 2;
363 blockb = (b[0] + b[4] + b[inpitch+0] + b[inpitch+4]) >> 2;
365 out[0 ] = cls.capturevideo.yuvnormalizetable[1][cls.capturevideo.rgbtoyuvscaletable[1][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[1][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[1][2][blockb] + 128];
367 out[outoffset] = cls.capturevideo.yuvnormalizetable[2][cls.capturevideo.rgbtoyuvscaletable[2][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[2][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[2][2][blockb] + 128];
373 static void SCR_CaptureVideo_Avi_VideoFrames(int num)
375 LOAD_FORMATSPECIFIC_AVI();
376 int x = 0, width = cls.capturevideo.width, height = cls.capturevideo.height;
377 unsigned char *in, *out;
378 // FIXME: width/height must be multiple of 2, enforce this?
379 in = cls.capturevideo.outbuffer;
380 out = cls.capturevideo.outbuffer + width*height*4;
381 SCR_CaptureVideo_ConvertFrame_BGRA_to_I420_flip(width, height, in, out);
382 x = width*height+(width/2)*(height/2)*2;
387 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
388 SCR_CaptureVideo_RIFF_IndexEntry("00dc", x, 0x10); // AVIIF_KEYFRAME
393 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX", 12+8+x);
394 SCR_CaptureVideo_RIFF_Push("LIST", "movi", 8+x);
396 SCR_CaptureVideo_RIFF_Push("00dc", NULL, x);
397 SCR_CaptureVideo_RIFF_WriteBytes(out, x);
398 SCR_CaptureVideo_RIFF_Pop();
401 SCR_CaptureVideo_RIFF_Pop();
402 SCR_CaptureVideo_RIFF_Pop();
407 static void SCR_CaptureVideo_Avi_EndVideo(void)
409 LOAD_FORMATSPECIFIC_AVI();
413 // close any open chunks
414 SCR_CaptureVideo_RIFF_Finish(true);
416 // go back and fix the video frames and audio samples fields
417 if(format->videofile_totalframes_offset1)
418 if(FS_Seek(cls.capturevideo.videofile, format->videofile_totalframes_offset1, SEEK_SET) >= 0)
420 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
421 SCR_CaptureVideo_RIFF_FlushNoIncrease();
423 if(format->videofile_totalframes_offset2)
424 if(FS_Seek(cls.capturevideo.videofile, format->videofile_totalframes_offset2, SEEK_SET) >= 0)
426 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.frame);
427 SCR_CaptureVideo_RIFF_FlushNoIncrease();
429 if (cls.capturevideo.soundrate)
431 if(format->videofile_totalsampleframes_offset)
432 if(FS_Seek(cls.capturevideo.videofile, format->videofile_totalsampleframes_offset, SEEK_SET) >= 0)
434 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundsampleframe);
435 SCR_CaptureVideo_RIFF_FlushNoIncrease();
440 if (format->riffindexbuffer.data)
442 Mem_Free(format->riffindexbuffer.data);
443 format->riffindexbuffer.data = NULL;
446 FS_Close(cls.capturevideo.videofile);
447 cls.capturevideo.videofile = NULL;
452 static void SCR_CaptureVideo_Avi_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length)
454 LOAD_FORMATSPECIFIC_AVI();
456 unsigned char bufstereo16le[PAINTBUFFER_SIZE * 4];
457 unsigned char* out_ptr;
460 // write the sound buffer as little endian 16bit interleaved stereo
461 for(i = 0, out_ptr = bufstereo16le; i < length; i++, out_ptr += 4)
465 n0 = paintbuffer[i].sample[0] * 32768.0f;
466 n0 = bound(-32768, n0, 32767);
467 out_ptr[0] = (unsigned char)n0;
468 out_ptr[1] = (unsigned char)(n0 >> 8);
470 n1 = paintbuffer[i].sample[1] * 32768.0f;
471 n1 = bound(-32768, n1, 32767);
472 out_ptr[2] = (unsigned char)n1;
473 out_ptr[3] = (unsigned char)(n1 >> 8);
479 SCR_CaptureVideo_RIFF_OverflowCheck(8 + x);
480 SCR_CaptureVideo_RIFF_IndexEntry("01wb", x, 0x10); // AVIIF_KEYFRAME
485 SCR_CaptureVideo_RIFF_Push("RIFF", "AVIX", 12+8+x);
486 SCR_CaptureVideo_RIFF_Push("LIST", "movi", 8+x);
488 SCR_CaptureVideo_RIFF_Push("01wb", NULL, x);
489 SCR_CaptureVideo_RIFF_WriteBytes(bufstereo16le, x);
490 SCR_CaptureVideo_RIFF_Pop();
493 SCR_CaptureVideo_RIFF_Pop();
494 SCR_CaptureVideo_RIFF_Pop();
498 void SCR_CaptureVideo_Avi_BeginVideo(void)
500 int width = cls.capturevideo.width;
501 int height = cls.capturevideo.height;
507 aspect = vid.width / (vid.height * vid_pixelheight.value);
509 cls.capturevideo.format = CAPTUREVIDEOFORMAT_AVI_I420;
510 cls.capturevideo.formatextension = "avi";
511 cls.capturevideo.videofile = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "%s.%s", cls.capturevideo.basename, cls.capturevideo.formatextension), "wb", false);
512 cls.capturevideo.endvideo = SCR_CaptureVideo_Avi_EndVideo;
513 cls.capturevideo.videoframes = SCR_CaptureVideo_Avi_VideoFrames;
514 cls.capturevideo.soundframe = SCR_CaptureVideo_Avi_SoundFrame;
515 cls.capturevideo.formatspecific = Mem_Alloc(tempmempool, sizeof(capturevideostate_avi_formatspecific_t));
517 LOAD_FORMATSPECIFIC_AVI();
518 format->canseek = (FS_Seek(cls.capturevideo.videofile, 0, SEEK_SET) == 0);
519 SCR_CaptureVideo_RIFF_Start();
520 // enclosing RIFF chunk (there can be multiple of these in >1GB files, the later ones are "AVIX" instead of "AVI " and have no header/stream info)
521 SCR_CaptureVideo_RIFF_Push("RIFF", "AVI ", format->canseek ? -1 : 12+(8+56+12+(12+52+8+40+8+68)+(cls.capturevideo.soundrate?(12+12+52+8+18):0)+12+(8+4))+12+(8+(((int) strlen(engineversion) | 1) + 1))+12);
523 SCR_CaptureVideo_RIFF_Push("LIST", "hdrl", format->canseek ? -1 : 8+56+12+(12+52+8+40+8+68)+(cls.capturevideo.soundrate?(12+12+52+8+18):0)+12+(8+4));
524 SCR_CaptureVideo_RIFF_Push("avih", NULL, 56);
525 SCR_CaptureVideo_RIFF_Write32((int)(1000000.0 / (cls.capturevideo.framerate / cls.capturevideo.framestep))); // microseconds per frame
526 SCR_CaptureVideo_RIFF_Write32(0); // max bytes per second
527 SCR_CaptureVideo_RIFF_Write32(0); // padding granularity
528 SCR_CaptureVideo_RIFF_Write32(0x910); // flags (AVIF_HASINDEX | AVIF_ISINTERLEAVED | AVIF_TRUSTCKTYPE)
529 format->videofile_firstchunkframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
530 SCR_CaptureVideo_RIFF_Write32(0); // total frames
531 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
532 if (cls.capturevideo.soundrate)
533 SCR_CaptureVideo_RIFF_Write32(2); // number of streams
535 SCR_CaptureVideo_RIFF_Write32(1); // number of streams
536 SCR_CaptureVideo_RIFF_Write32(0); // suggested buffer size
537 SCR_CaptureVideo_RIFF_Write32(width); // width
538 SCR_CaptureVideo_RIFF_Write32(height); // height
539 SCR_CaptureVideo_RIFF_Write32(0); // reserved[0]
540 SCR_CaptureVideo_RIFF_Write32(0); // reserved[1]
541 SCR_CaptureVideo_RIFF_Write32(0); // reserved[2]
542 SCR_CaptureVideo_RIFF_Write32(0); // reserved[3]
543 SCR_CaptureVideo_RIFF_Pop();
545 SCR_CaptureVideo_RIFF_Push("LIST", "strl", format->canseek ? -1 : 12+52+8+40+8+68);
546 SCR_CaptureVideo_RIFF_Push("strh", "vids", 52);
547 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // stream fourcc (I420 colorspace, uncompressed)
548 SCR_CaptureVideo_RIFF_Write32(0); // flags
549 SCR_CaptureVideo_RIFF_Write16(0); // priority
550 SCR_CaptureVideo_RIFF_Write16(0); // language
551 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
552 // find an ideal divisor for the framerate
553 FindFraction(cls.capturevideo.framerate / cls.capturevideo.framestep, &n, &d, 1000);
554 SCR_CaptureVideo_RIFF_Write32(d); // samples/second divisor
555 SCR_CaptureVideo_RIFF_Write32(n); // samples/second multiplied by divisor
556 SCR_CaptureVideo_RIFF_Write32(0); // start
557 format->videofile_totalframes_offset1 = SCR_CaptureVideo_RIFF_GetPosition();
558 SCR_CaptureVideo_RIFF_Write32(0xFFFFFFFF); // length
559 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // suggested buffer size
560 SCR_CaptureVideo_RIFF_Write32(0); // quality
561 SCR_CaptureVideo_RIFF_Write32(0); // sample size
562 SCR_CaptureVideo_RIFF_Write16(0); // frame left
563 SCR_CaptureVideo_RIFF_Write16(0); // frame top
564 SCR_CaptureVideo_RIFF_Write16(width); // frame right
565 SCR_CaptureVideo_RIFF_Write16(height); // frame bottom
566 SCR_CaptureVideo_RIFF_Pop();
567 // video stream format
568 SCR_CaptureVideo_RIFF_Push("strf", NULL, 40);
569 SCR_CaptureVideo_RIFF_Write32(40); // BITMAPINFO struct size
570 SCR_CaptureVideo_RIFF_Write32(width); // width
571 SCR_CaptureVideo_RIFF_Write32(height); // height
572 SCR_CaptureVideo_RIFF_Write16(3); // planes
573 SCR_CaptureVideo_RIFF_Write16(12); // bitcount
574 SCR_CaptureVideo_RIFF_WriteFourCC("I420"); // compression
575 SCR_CaptureVideo_RIFF_Write32(width*height+(width/2)*(height/2)*2); // size of image
576 SCR_CaptureVideo_RIFF_Write32(0); // x pixels per meter
577 SCR_CaptureVideo_RIFF_Write32(0); // y pixels per meter
578 SCR_CaptureVideo_RIFF_Write32(0); // color used
579 SCR_CaptureVideo_RIFF_Write32(0); // color important
580 SCR_CaptureVideo_RIFF_Pop();
584 SCR_CaptureVideo_RIFF_Push("indx", NULL, -1);
585 SCR_CaptureVideo_RIFF_Write16(4); // wLongsPerEntry
586 SCR_CaptureVideo_RIFF_Write16(0); // bIndexSubType=0, bIndexType=0
587 format->videofile_ix_master_video_inuse_offset = SCR_CaptureVideo_RIFF_GetPosition();
588 SCR_CaptureVideo_RIFF_Write32(0); // nEntriesInUse
589 SCR_CaptureVideo_RIFF_WriteFourCC("00dc"); // dwChunkId
590 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved1
591 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved2
592 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved3
593 format->videofile_ix_master_video_start_offset = SCR_CaptureVideo_RIFF_GetPosition();
594 for(i = 0; i < AVI_MASTER_INDEX_SIZE * 4; ++i)
595 SCR_CaptureVideo_RIFF_Write32(0); // fill up later
596 SCR_CaptureVideo_RIFF_Pop();
598 // extended format (aspect!)
599 SCR_CaptureVideo_RIFF_Push("vprp", NULL, 68);
600 SCR_CaptureVideo_RIFF_Write32(0); // VideoFormatToken
601 SCR_CaptureVideo_RIFF_Write32(0); // VideoStandard
602 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.framerate / cls.capturevideo.framestep)); // dwVerticalRefreshRate (bogus)
603 SCR_CaptureVideo_RIFF_Write32(width); // dwHTotalInT
604 SCR_CaptureVideo_RIFF_Write32(height); // dwVTotalInLines
605 FindFraction(aspect, &n, &d, 1000);
606 SCR_CaptureVideo_RIFF_Write32((n << 16) | d); // dwFrameAspectRatio // TODO a word
607 SCR_CaptureVideo_RIFF_Write32(width); // dwFrameWidthInPixels
608 SCR_CaptureVideo_RIFF_Write32(height); // dwFrameHeightInLines
609 SCR_CaptureVideo_RIFF_Write32(1); // nFieldPerFrame
610 SCR_CaptureVideo_RIFF_Write32(width); // CompressedBMWidth
611 SCR_CaptureVideo_RIFF_Write32(height); // CompressedBMHeight
612 SCR_CaptureVideo_RIFF_Write32(width); // ValidBMHeight
613 SCR_CaptureVideo_RIFF_Write32(height); // ValidBMWidth
614 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMXOffset
615 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMYOffset
616 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMXOffsetInT
617 SCR_CaptureVideo_RIFF_Write32(0); // ValidBMYValidStartLine
618 SCR_CaptureVideo_RIFF_Pop();
619 SCR_CaptureVideo_RIFF_Pop();
620 if (cls.capturevideo.soundrate)
623 SCR_CaptureVideo_RIFF_Push("LIST", "strl", format->canseek ? -1 : 12+52+8+18);
624 SCR_CaptureVideo_RIFF_Push("strh", "auds", 52);
625 SCR_CaptureVideo_RIFF_Write32(1); // stream fourcc (PCM audio, uncompressed)
626 SCR_CaptureVideo_RIFF_Write32(0); // flags
627 SCR_CaptureVideo_RIFF_Write16(0); // priority
628 SCR_CaptureVideo_RIFF_Write16(0); // language
629 SCR_CaptureVideo_RIFF_Write32(0); // initial frames
630 SCR_CaptureVideo_RIFF_Write32(1); // samples/second divisor
631 SCR_CaptureVideo_RIFF_Write32((int)(cls.capturevideo.soundrate)); // samples/second multiplied by divisor
632 SCR_CaptureVideo_RIFF_Write32(0); // start
633 format->videofile_totalsampleframes_offset = SCR_CaptureVideo_RIFF_GetPosition();
634 SCR_CaptureVideo_RIFF_Write32(0xFFFFFFFF); // length
635 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 2); // suggested buffer size (this is a half second)
636 SCR_CaptureVideo_RIFF_Write32(0); // quality
637 SCR_CaptureVideo_RIFF_Write32(4); // sample size
638 SCR_CaptureVideo_RIFF_Write16(0); // frame left
639 SCR_CaptureVideo_RIFF_Write16(0); // frame top
640 SCR_CaptureVideo_RIFF_Write16(0); // frame right
641 SCR_CaptureVideo_RIFF_Write16(0); // frame bottom
642 SCR_CaptureVideo_RIFF_Pop();
643 // audio stream format
644 SCR_CaptureVideo_RIFF_Push("strf", NULL, 18);
645 SCR_CaptureVideo_RIFF_Write16(1); // format (uncompressed PCM?)
646 SCR_CaptureVideo_RIFF_Write16(2); // channels (stereo)
647 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate); // sampleframes per second
648 SCR_CaptureVideo_RIFF_Write32(cls.capturevideo.soundrate * 4); // average bytes per second
649 SCR_CaptureVideo_RIFF_Write16(4); // block align
650 SCR_CaptureVideo_RIFF_Write16(16); // bits per sample
651 SCR_CaptureVideo_RIFF_Write16(0); // size
652 SCR_CaptureVideo_RIFF_Pop();
656 SCR_CaptureVideo_RIFF_Push("indx", NULL, -1);
657 SCR_CaptureVideo_RIFF_Write16(4); // wLongsPerEntry
658 SCR_CaptureVideo_RIFF_Write16(0); // bIndexSubType=0, bIndexType=0
659 format->videofile_ix_master_audio_inuse_offset = SCR_CaptureVideo_RIFF_GetPosition();
660 SCR_CaptureVideo_RIFF_Write32(0); // nEntriesInUse
661 SCR_CaptureVideo_RIFF_WriteFourCC("01wb"); // dwChunkId
662 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved1
663 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved2
664 SCR_CaptureVideo_RIFF_Write32(0); // dwReserved3
665 format->videofile_ix_master_audio_start_offset = SCR_CaptureVideo_RIFF_GetPosition();
666 for(i = 0; i < AVI_MASTER_INDEX_SIZE * 4; ++i)
667 SCR_CaptureVideo_RIFF_Write32(0); // fill up later
668 SCR_CaptureVideo_RIFF_Pop();
670 SCR_CaptureVideo_RIFF_Pop();
673 format->videofile_ix_master_audio_inuse = format->videofile_ix_master_video_inuse = 0;
675 // extended header (for total #frames)
676 SCR_CaptureVideo_RIFF_Push("LIST", "odml", 8+4);
677 SCR_CaptureVideo_RIFF_Push("dmlh", NULL, 4);
678 format->videofile_totalframes_offset2 = SCR_CaptureVideo_RIFF_GetPosition();
679 SCR_CaptureVideo_RIFF_Write32(0xFFFFFFFF);
680 SCR_CaptureVideo_RIFF_Pop();
681 SCR_CaptureVideo_RIFF_Pop();
683 // close the AVI header list
684 SCR_CaptureVideo_RIFF_Pop();
685 // software that produced this AVI video file
686 SCR_CaptureVideo_RIFF_Push("LIST", "INFO", 8+((strlen(engineversion) | 1) + 1));
687 SCR_CaptureVideo_RIFF_Push("ISFT", NULL, strlen(engineversion) + 1);
688 SCR_CaptureVideo_RIFF_WriteTerminatedString(engineversion);
689 SCR_CaptureVideo_RIFF_Pop();
690 // enable this junk filler if you like the LIST movi to always begin at 4KB in the file (why?)
692 SCR_CaptureVideo_RIFF_Push("JUNK", NULL);
693 x = 4096 - SCR_CaptureVideo_RIFF_GetPosition();
696 const char *junkfiller = "[ DarkPlaces junk data ]";
697 int i = min(x, (int)strlen(junkfiller));
698 SCR_CaptureVideo_RIFF_WriteBytes((const unsigned char *)junkfiller, i);
701 SCR_CaptureVideo_RIFF_Pop();
703 SCR_CaptureVideo_RIFF_Pop();
704 // begin the actual video section now
705 SCR_CaptureVideo_RIFF_Push("LIST", "movi", format->canseek ? -1 : 0);
706 format->videofile_ix_movistart = format->riffstackstartoffset[1];
707 // we're done with the headers now...
708 SCR_CaptureVideo_RIFF_Flush();
709 if (format->riffstacklevel != 2)
710 Sys_Error("SCR_CaptureVideo_BeginVideo: broken AVI writing code (stack level is %i (should be 2) at end of headers)\n", format->riffstacklevel);
714 // close the movi immediately
715 SCR_CaptureVideo_RIFF_Pop();
716 // close the AVI immediately (we'll put all frames into AVIX)
717 SCR_CaptureVideo_RIFF_Pop();