+ LOAD_FORMATSPECIFIC_OGG();
+ ogg_page pg;
+ ogg_packet pt;
+
+ // repeat the last frame so we can set the end-of-stream flag
+ qtheora_encode_YUVin(&format->ts, &format->yuv);
+ qtheora_encode_packetout(&format->ts, true, &pt);
+ qogg_stream_packetin(&format->to, &pt);
+
+ if(cls.capturevideo.soundrate)
+ {
+ qvorbis_analysis_wrote(&format->vd, 0);
+ while(qvorbis_analysis_blockout(&format->vd, &format->vb) == 1)
+ {
+ qvorbis_analysis(&format->vb, NULL);
+ qvorbis_bitrate_addblock(&format->vb);
+ while(qvorbis_bitrate_flushpacket(&format->vd, &pt))
+ qogg_stream_packetin(&format->vo, &pt);
+ }
+ }
+
+ while(qogg_stream_pageout(&format->to, &pg) > 0)
+ {
+ FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+ FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+ }
+
+ if(cls.capturevideo.soundrate)
+ {
+ while(qogg_stream_pageout(&format->vo, &pg) > 0)
+ {
+ FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+ FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+ }
+ }
+
+ while (1) {
+ int result = qogg_stream_flush (&format->to, &pg);
+ if (result < 0)
+ fprintf (stderr, "Internal Ogg library error.\n"); // TODO Host_Error
+ if (result <= 0)
+ break;
+ FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+ FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+ }
+
+ if(cls.capturevideo.soundrate)
+ {
+ while (1) {
+ int result = qogg_stream_flush (&format->vo, &pg);
+ if (result < 0)
+ fprintf (stderr, "Internal Ogg library error.\n"); // TODO Host_Error
+ if (result <= 0)
+ break;
+ FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+ FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+ }
+
+ qogg_stream_clear(&format->vo);
+ qvorbis_block_clear(&format->vb);
+ qvorbis_dsp_clear(&format->vd);
+ }
+
+ qogg_stream_clear(&format->to);
+ qtheora_clear(&format->ts);
+ qvorbis_info_clear(&format->vi);
+
+ Mem_Free(format->yuv.y);
+ Mem_Free(format->yuv.u);
+ Mem_Free(format->yuv.v);
+ Mem_Free(format);
+
+ FS_Close(cls.capturevideo.videofile);
+ cls.capturevideo.videofile = NULL;
+}
+
+static void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV()
+{
+ LOAD_FORMATSPECIFIC_OGG();
+ int x, y;
+ int blockr, blockg, blockb;
+ unsigned char *b = cls.capturevideo.outbuffer;
+ int w = cls.capturevideo.width;
+ int h = cls.capturevideo.height;
+ int inpitch = w*4;
+
+ for(y = 0; y < h; ++y)
+ {
+ for(b = cls.capturevideo.outbuffer + (h-1-y)*w*4, x = 0; x < w; ++x)
+ {
+ blockr = b[2];
+ blockg = b[1];
+ blockb = b[0];
+ format->yuv.y[x + format->yuv.y_stride * y] =
+ cls.capturevideo.yuvnormalizetable[0][cls.capturevideo.rgbtoyuvscaletable[0][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[0][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[0][2][blockb]];
+ b += 4;
+ }
+
+ if((y & 1) == 0)
+ {
+ for(b = cls.capturevideo.outbuffer + (h-2-y)*w*4, x = 0; x < w/2; ++x)
+ {
+ blockr = (b[2] + b[6] + b[inpitch+2] + b[inpitch+6]) >> 2;
+ blockg = (b[1] + b[5] + b[inpitch+1] + b[inpitch+5]) >> 2;
+ blockb = (b[0] + b[4] + b[inpitch+0] + b[inpitch+4]) >> 2;
+ format->yuv.u[x + format->yuv.uv_stride * (y/2)] =
+ cls.capturevideo.yuvnormalizetable[1][cls.capturevideo.rgbtoyuvscaletable[1][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[1][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[1][2][blockb] + 128];
+ format->yuv.v[x + format->yuv.uv_stride * (y/2)] =
+ cls.capturevideo.yuvnormalizetable[2][cls.capturevideo.rgbtoyuvscaletable[2][0][blockr] + cls.capturevideo.rgbtoyuvscaletable[2][1][blockg] + cls.capturevideo.rgbtoyuvscaletable[2][2][blockb] + 128];
+ b += 8;
+ }
+ }
+ }
+}
+
+static void SCR_CaptureVideo_Ogg_VideoFrames(int num)
+{
+ LOAD_FORMATSPECIFIC_OGG();
+ ogg_page pg;
+ ogg_packet pt;
+
+ // data is in cls.capturevideo.outbuffer as BGRA and has size width*height
+
+ SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV();
+
+ while(num-- > 0)
+ {
+ qtheora_encode_YUVin(&format->ts, &format->yuv);
+ qtheora_encode_packetout(&format->ts, false, &pt);
+ qogg_stream_packetin(&format->to, &pt);
+
+ while(qogg_stream_pageout(&format->to, &pg) > 0)
+ {
+ FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+ FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+ }
+ }
+}
+
+static void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length)
+{
+ LOAD_FORMATSPECIFIC_OGG();
+ float **vorbis_buffer;
+ size_t i;
+ int j;
+ ogg_page pg;
+ ogg_packet pt;
+
+ vorbis_buffer = qvorbis_analysis_buffer(&format->vd, length);
+ for(i = 0; i < length; ++i)
+ {
+ for(j = 0; j < cls.capturevideo.soundchannels; ++j)
+ vorbis_buffer[j][i] = paintbuffer[i].sample[j] / 32768.0f;
+ }
+ qvorbis_analysis_wrote(&format->vd, length);
+
+ while(qvorbis_analysis_blockout(&format->vd, &format->vb) == 1)
+ {
+ qvorbis_analysis(&format->vb, NULL);
+ qvorbis_bitrate_addblock(&format->vb);
+
+ while(qvorbis_bitrate_flushpacket(&format->vd, &pt))
+ qogg_stream_packetin(&format->vo, &pt);
+ }
+
+ while(qogg_stream_pageout(&format->vo, &pg) > 0)
+ {
+ FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+ FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+ }
+}
+
+void SCR_CaptureVideo_Ogg_BeginVideo()
+{
+ cls.capturevideo.format = CAPTUREVIDEOFORMAT_OGG_VORBIS_THEORA;