]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cap_ogg.c
set rsurface.entity correctly before calling R_GetCurrentTexture
[xonotic/darkplaces.git] / cap_ogg.c
index 00dc3cbb70cc7d9c2f5ff59ea7ec6c5a944eb64c..159457d95e55f58675e0497f55909679b6bbaee1 100644 (file)
--- a/cap_ogg.c
+++ b/cap_ogg.c
 static cvar_t cl_capturevideo_ogg_theora_quality = {CVAR_SAVE, "cl_capturevideo_ogg_theora_quality", "32", "video quality factor (0 to 63), or -1 to use bitrate only; higher is better"};
 static cvar_t cl_capturevideo_ogg_theora_bitrate = {CVAR_SAVE, "cl_capturevideo_ogg_theora_bitrate", "-1", "video bitrate (45 to 2000 kbps), or -1 to use quality only; higher is better"};
 static cvar_t cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier = {CVAR_SAVE, "cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier", "1.5", "how much more bit rate to use for keyframes, specified as a factor of at least 1"};
-static cvar_t cl_capturevideo_ogg_theora_keyframe_frequency = {CVAR_SAVE, "cl_capturevideo_ogg_theora_keyframe_frequency", "64", "maximum number of frames between two key frames (1 to 1000)"};
-static cvar_t cl_capturevideo_ogg_theora_keyframe_mindistance = {CVAR_SAVE, "cl_capturevideo_ogg_theora_keyframe_mindistance", "8", "minimum number of frames between two key frames (1 to 1000)"};
+static cvar_t cl_capturevideo_ogg_theora_keyframe_maxinterval = {CVAR_SAVE, "cl_capturevideo_ogg_theora_keyframe_maxinterval", "64", "maximum keyframe interval (1 to 1000)"};
+static cvar_t cl_capturevideo_ogg_theora_keyframe_mininterval = {CVAR_SAVE, "cl_capturevideo_ogg_theora_keyframe_mininterval", "8", "minimum keyframe interval (1 to 1000)"};
 static cvar_t cl_capturevideo_ogg_theora_keyframe_auto_threshold = {CVAR_SAVE, "cl_capturevideo_ogg_theora_keyframe_auto_threshold", "80", "threshold for key frame decision (0 to 100)"};
 static cvar_t cl_capturevideo_ogg_theora_noise_sensitivity = {CVAR_SAVE, "cl_capturevideo_ogg_theora_noise_sensitivity", "1", "video noise sensitivity (0 to 6); lower is better"};
 static cvar_t cl_capturevideo_ogg_theora_sharpness = {CVAR_SAVE, "cl_capturevideo_ogg_theora_sharpness", "0", "sharpness (0 to 2); lower is sharper"};
-static cvar_t cl_capturevideo_ogg_vorbis_quality = {CVAR_SAVE, "cl_capturevideo_ogg_vorbis_quality", "1", "audio quality (-1 to 10); higher is better"};
+static cvar_t cl_capturevideo_ogg_vorbis_quality = {CVAR_SAVE, "cl_capturevideo_ogg_vorbis_quality", "3", "audio quality (-1 to 10); higher is better"};
 
 // ogg.h stuff
 #ifdef _MSC_VER
@@ -597,8 +597,8 @@ void SCR_CaptureVideo_Ogg_Init()
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_quality);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_bitrate);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier);
-       Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_frequency);
-       Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_mindistance);
+       Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_maxinterval);
+       Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_mininterval);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_auto_threshold);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_noise_sensitivity);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_vorbis_quality);
@@ -617,6 +617,21 @@ void SCR_CaptureVideo_Ogg_CloseDLL()
        Sys_UnloadLibrary (&og_dll);
 }
 
+// this struct should not be needed
+// however, libogg appears to pull the ogg_page's data element away from our
+// feet before we get to write the data due to interleaving
+// so this struct is used to keep the page data around until it actually gets
+// written
+typedef struct allocatedoggpage_s
+{
+       size_t len;
+       double time;
+       unsigned char data[65307];
+       // this number is from RFC 3533. In case libogg writes more, we'll have to increase this
+       // but we'll get a Host_Error in this case so we can track it down
+}
+allocatedoggpage_t;
+
 typedef struct capturevideostate_ogg_formatspecific_s
 {
        ogg_stream_state to, vo;
@@ -630,38 +645,22 @@ typedef struct capturevideostate_ogg_formatspecific_s
        int lastnum;
        int channels;
 
-       // for interleaving
-       ogg_page videopage;
-       ogg_page audiopage;
-       qboolean have_videopage;
-       qboolean have_audiopage;
+       allocatedoggpage_t videopage, audiopage;
 }
 capturevideostate_ogg_formatspecific_t;
 #define LOAD_FORMATSPECIFIC_OGG() capturevideostate_ogg_formatspecific_t *format = (capturevideostate_ogg_formatspecific_t *) cls.capturevideo.formatspecific
 
-#define INTERLEAVING_NOT_WORKING
 static void SCR_CaptureVideo_Ogg_Interleave()
 {
        LOAD_FORMATSPECIFIC_OGG();
-
-       //fprintf(stderr, "<");
+       ogg_page pg;
 
        if(!cls.capturevideo.soundrate)
        {
-               for(;;)
+               while(qogg_stream_pageout(&format->to, &pg) > 0)
                {
-                       // first: make sure we have a page of both types
-                       if(!format->have_videopage)
-                               if(qogg_stream_pageout(&format->to, &format->videopage) > 0)
-                                       format->have_videopage = true;
-                       if(format->have_videopage)
-                       {
-                               FS_Write(cls.capturevideo.videofile, format->videopage.header, format->videopage.header_len);
-                               FS_Write(cls.capturevideo.videofile, format->videopage.body, format->videopage.body_len);
-                               format->have_videopage = false;
-                       }
-                       else
-                               break;
+                       FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+                       FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
                }
                return;
        }
@@ -669,69 +668,44 @@ static void SCR_CaptureVideo_Ogg_Interleave()
        for(;;)
        {
                // first: make sure we have a page of both types
-               if(!format->have_videopage)
-                       if(qogg_stream_pageout(&format->to, &format->videopage) > 0)
+               if(!format->videopage.len)
+                       if(qogg_stream_pageout(&format->to, &pg) > 0)
                        {
-                               //fprintf(stderr, "V");
-                               format->have_videopage = true;
-
-#ifdef INTERLEAVING_NOT_WORKING
-                               // why do I have to do this? the code should work without the
-                               // following three lines, which turn this attempt at correct
-                               // interleaving back into the old stupid one that oggz-validate
-                               // hates
-                               FS_Write(cls.capturevideo.videofile, format->videopage.header, format->videopage.header_len);
-                               FS_Write(cls.capturevideo.videofile, format->videopage.body, format->videopage.body_len);
-                               format->have_videopage = false;
-                               continue;
-#endif
+                               format->videopage.len = pg.header_len + pg.body_len;
+                               format->videopage.time = qtheora_granule_time(&format->ts, qogg_page_granulepos(&pg));
+                               if(format->videopage.len > sizeof(format->videopage.data))
+                                       Host_Error("video page too long");
+                               memcpy(format->videopage.data, pg.header, pg.header_len);
+                               memcpy(format->videopage.data + pg.header_len, pg.body, pg.body_len);
                        }
-               if(!format->have_audiopage)
-                       if(qogg_stream_pageout(&format->vo, &format->audiopage) > 0)
+               if(!format->audiopage.len)
+                       if(qogg_stream_pageout(&format->vo, &pg) > 0)
                        {
-                               //fprintf(stderr, "A");
-                               format->have_audiopage = true;
-
-#ifdef INTERLEAVING_NOT_WORKING
-                               // why do I have to do this? the code should work without the
-                               // following three lines, which turn this attempt at correct
-                               // interleaving back into the old stupid one that oggz-validate
-                               // hates
-                               FS_Write(cls.capturevideo.videofile, format->audiopage.header, format->audiopage.header_len);
-                               FS_Write(cls.capturevideo.videofile, format->audiopage.body, format->audiopage.body_len);
-                               format->have_audiopage = false;
-                               continue;
-#endif
+                               format->audiopage.len = pg.header_len + pg.body_len;
+                               format->audiopage.time = qvorbis_granule_time(&format->vd, qogg_page_granulepos(&pg));
+                               if(format->audiopage.len > sizeof(format->audiopage.data))
+                                       Host_Error("audio page too long");
+                               memcpy(format->audiopage.data, pg.header, pg.header_len);
+                               memcpy(format->audiopage.data + pg.header_len, pg.body, pg.body_len);
                        }
 
-               if(format->have_videopage && format->have_audiopage)
+               if(format->videopage.len && format->audiopage.len)
                {
                        // output the page that ends first
-                       double audiotime = qvorbis_granule_time(&format->vd, qogg_page_granulepos(&format->audiopage));
-                       double videotime = qtheora_granule_time(&format->ts, qogg_page_granulepos(&format->videopage));
-                       //fprintf(stderr, "(A=%f V=%f)\n", audiotime, videotime);
-                       if(audiotime < videotime)
+                       if(format->videopage.time < format->audiopage.time)
                        {
-                               FS_Write(cls.capturevideo.videofile, format->audiopage.header, format->audiopage.header_len);
-                               FS_Write(cls.capturevideo.videofile, format->audiopage.body, format->audiopage.body_len);
-                               format->have_audiopage = false;
-
-                               //fprintf(stderr, "a");
+                               FS_Write(cls.capturevideo.videofile, format->videopage.data, format->videopage.len);
+                               format->videopage.len = 0;
                        }
                        else
                        {
-                               FS_Write(cls.capturevideo.videofile, format->videopage.header, format->videopage.header_len);
-                               FS_Write(cls.capturevideo.videofile, format->videopage.body, format->videopage.body_len);
-                               format->have_videopage = false;
-
-                               //fprintf(stderr, "v");
+                               FS_Write(cls.capturevideo.videofile, format->audiopage.data, format->audiopage.len);
+                               format->audiopage.len = 0;
                        }
                }
                else
                        break;
        }
-
-       //fprintf(stderr, ">");
 }
 
 static void SCR_CaptureVideo_Ogg_FlushInterleaving()
@@ -739,18 +713,16 @@ static void SCR_CaptureVideo_Ogg_FlushInterleaving()
        LOAD_FORMATSPECIFIC_OGG();
 
        if(cls.capturevideo.soundrate)
-       if(format->have_audiopage)
+       if(format->audiopage.len)
        {
-               FS_Write(cls.capturevideo.videofile, format->audiopage.header, format->audiopage.header_len);
-               FS_Write(cls.capturevideo.videofile, format->audiopage.body, format->audiopage.body_len);
-               format->have_audiopage = false;
+               FS_Write(cls.capturevideo.videofile, format->audiopage.data, format->audiopage.len);
+               format->audiopage.len = 0;
        }
 
-       if(format->have_videopage)
+       if(format->videopage.len)
        {
-               FS_Write(cls.capturevideo.videofile, format->videopage.header, format->videopage.header_len);
-               FS_Write(cls.capturevideo.videofile, format->videopage.body, format->videopage.body_len);
-               format->have_videopage = false;
+               FS_Write(cls.capturevideo.videofile, format->videopage.data, format->videopage.len);
+               format->videopage.len = 0;
        }
 }
 
@@ -917,6 +889,19 @@ static void SCR_CaptureVideo_Ogg_VideoFrames(int num)
        // TODO maybe send num-1 frames from here already
 }
 
+typedef int channelmapping_t[8];
+channelmapping_t mapping[8] =
+{
+       { 0, -1, -1, -1, -1, -1, -1, -1 }, // mono
+       { 0, 1, -1, -1, -1, -1, -1, -1 }, // stereo
+       { 0, 1, 2, -1, -1, -1, -1, -1 }, // L C R
+       { 0, 1, 2, 3, -1, -1, -1, -1 }, // surround40
+       { 0, 2, 3, 4, 1, -1, -1, -1 }, // FL FC FR RL RR
+       { 0, 2, 3, 4, 1, 5, -1, -1 }, // surround51
+       { 0, 2, 3, 4, 1, 5, 6, -1 }, // (not defined by vorbis spec)
+       { 0, 2, 3, 4, 1, 5, 6, 7 } // surround71 (not defined by vorbis spec)
+};
+
 static void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length)
 {
        LOAD_FORMATSPECIFIC_OGG();
@@ -924,12 +909,14 @@ static void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintb
        size_t i;
        int j;
        ogg_packet pt;
+       int *map = mapping[bound(1, cls.capturevideo.soundchannels, 8) - 1];
 
        vorbis_buffer = qvorbis_analysis_buffer(&format->vd, length);
-       for(i = 0; i < length; ++i)
+       for(j = 0; j < cls.capturevideo.soundchannels; ++j)
        {
-               for(j = 0; j < cls.capturevideo.soundchannels; ++j)
-                       vorbis_buffer[j][i] = paintbuffer[i].sample[j] / 32768.0f;
+               float *b = vorbis_buffer[map[j]];
+               for(i = 0; i < length; ++i)
+                       b[i] = paintbuffer[i].sample[j] / 32768.0f;
        }
        qvorbis_analysis_wrote(&format->vd, length);
 
@@ -948,7 +935,8 @@ static void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintb
 void SCR_CaptureVideo_Ogg_BeginVideo()
 {
        cls.capturevideo.format = CAPTUREVIDEOFORMAT_OGG_VORBIS_THEORA;
-       cls.capturevideo.videofile = FS_OpenRealFile(va("%s.ogv", cls.capturevideo.basename), "wb", false);
+       cls.capturevideo.formatextension = "ogv";
+       cls.capturevideo.videofile = FS_OpenRealFile(va("%s.%s", cls.capturevideo.basename, cls.capturevideo.formatextension), "wb", false);
        cls.capturevideo.endvideo = SCR_CaptureVideo_Ogg_EndVideo;
        cls.capturevideo.videoframes = SCR_CaptureVideo_Ogg_VideoFrames;
        cls.capturevideo.soundframe = SCR_CaptureVideo_Ogg_SoundFrame;
@@ -975,7 +963,7 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                        qogg_stream_init(&format->vo, format->serial2);
                }
 
-               format->have_videopage = format->have_audiopage = false;
+               format->videopage.len = format->audiopage.len = 0;
 
                qtheora_info_init(&ti);
                ti.frame_width = cls.capturevideo.width;
@@ -993,9 +981,9 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                        format->yuv[i].uv_width = ti.width / 2;
                        format->yuv[i].uv_height = ti.height / 2;
                        format->yuv[i].uv_stride = ti.width / 2;
-                       format->yuv[i].y = Mem_Alloc(tempmempool, format->yuv[i].y_stride * format->yuv[i].y_height);
-                       format->yuv[i].u = Mem_Alloc(tempmempool, format->yuv[i].uv_stride * format->yuv[i].uv_height);
-                       format->yuv[i].v = Mem_Alloc(tempmempool, format->yuv[i].uv_stride * format->yuv[i].uv_height);
+                       format->yuv[i].y = (unsigned char *) Mem_Alloc(tempmempool, format->yuv[i].y_stride * format->yuv[i].y_height);
+                       format->yuv[i].u = (unsigned char *) Mem_Alloc(tempmempool, format->yuv[i].uv_stride * format->yuv[i].uv_height);
+                       format->yuv[i].v = (unsigned char *) Mem_Alloc(tempmempool, format->yuv[i].uv_stride * format->yuv[i].uv_height);
                }
                format->yuvi = -1; // -1: no frame valid yet, write into 0
 
@@ -1036,25 +1024,26 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                        if(ti.quality < 0)
                        {
                                ti.target_bitrate = bound(45000, ti.target_bitrate, 2000000);
-                               ti.keyframe_data_target_bitrate = ti.target_bitrate * max(1, cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier.value);
+                               ti.keyframe_data_target_bitrate = (int) (ti.target_bitrate * max(1, cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier.value));
                                ti.quality = -1;
                        }
                        else
                        {
                                ti.target_bitrate = bound(45000, ti.target_bitrate, 2000000);
-                               ti.keyframe_data_target_bitrate = ti.target_bitrate * max(1, cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier.value);
+                               ti.keyframe_data_target_bitrate = (int) (ti.target_bitrate * max(1, cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier.value));
                                ti.quality = -1;
                        }
                }
 
-               ti.keyframe_frequency = bound(1, cl_capturevideo_ogg_theora_keyframe_frequency.integer, 1000);
-               ti.keyframe_mindistance = bound(1, cl_capturevideo_ogg_theora_keyframe_mindistance.integer, (int) ti.keyframe_frequency);
+               // this -1 magic is because ti.keyframe_frequency and ti.keyframe_mindistance use different metrics
+               ti.keyframe_frequency = bound(1, cl_capturevideo_ogg_theora_keyframe_maxinterval.integer, 1000);
+               ti.keyframe_mindistance = bound(1, cl_capturevideo_ogg_theora_keyframe_mininterval.integer, (int) ti.keyframe_frequency) - 1;
                ti.noise_sensitivity = bound(0, cl_capturevideo_ogg_theora_noise_sensitivity.integer, 6);
                ti.sharpness = bound(0, cl_capturevideo_ogg_theora_sharpness.integer, 2);
                ti.keyframe_auto_threshold = bound(0, cl_capturevideo_ogg_theora_keyframe_auto_threshold.integer, 100);
 
                ti.keyframe_frequency_force = ti.keyframe_frequency;
-               ti.keyframe_auto_p = (ti.keyframe_frequency != ti.keyframe_mindistance);
+               ti.keyframe_auto_p = (ti.keyframe_frequency != ti.keyframe_mindistance + 1);
 
                qtheora_encode_init(&format->ts, &ti);
                qtheora_info_clear(&ti);