]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cap_ogg.c
MSG_ReadString: if hitting the max length of the string, continue to read until NUL...
[xonotic/darkplaces.git] / cap_ogg.c
index f9e0ab4e6b90cc0150977816de24323bb09788b6..eef3f90d6e3503b5febb2172d58ca318915a6389 100644 (file)
--- a/cap_ogg.c
+++ b/cap_ogg.c
@@ -8,8 +8,9 @@
 #include "cap_ogg.h"
 
 // video capture cvars
-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_vp3compat = {CVAR_SAVE, "cl_capturevideo_ogg_theora_vp3compat", "1", "make VP3 compatible theora streams"};
+static cvar_t cl_capturevideo_ogg_theora_quality = {CVAR_SAVE, "cl_capturevideo_ogg_theora_quality", "48", "video quality factor (0 to 63), or -1 to use bitrate only; higher is better; setting both to -1 achieves unlimited quality"};
+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; setting both to -1 achieves unlimited quality"};
 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_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)"};
@@ -310,6 +311,9 @@ static int (*qvorbis_encode_init_vbr) (vorbis_info *vi,
 // end of vorbisenc.h stuff
 
 // theora.h stuff
+
+#define TH_ENCCTL_SET_VP3_COMPATIBLE (10)
+
 typedef struct {
     int   y_width;      /**< Width of the Y' luminance plane */
     int   y_height;     /**< Height of the luminance plane */
@@ -345,7 +349,7 @@ typedef enum {
   OC_PF_420,    /**< Chroma subsampling by 2 in each direction (4:2:0) */
   OC_PF_RSVD,   /**< Reserved value */
   OC_PF_422,    /**< Horizonatal chroma subsampling by 2 (4:2:2) */
-  OC_PF_444,    /**< No chroma subsampling at all (4:4:4) */
+  OC_PF_444     /**< No chroma subsampling at all (4:4:4) */
 } theora_pixelformat;
 /**
  * Theora bitstream info.
@@ -457,6 +461,7 @@ static void (*qtheora_clear) (theora_state *t);
 static void (*qtheora_comment_init) (theora_comment *tc);
 static void  (*qtheora_comment_clear) (theora_comment *tc);
 static double (*qtheora_granule_time) (theora_state *th,ogg_int64_t granulepos);
+static int (*qtheora_control) (theora_state *th,int req,void *buf,size_t buf_sz);
 // end of theora.h stuff
 
 static dllfunction_t oggfuncs[] =
@@ -512,18 +517,18 @@ static dllfunction_t theorafuncs[] =
        {"theora_encode_tables", (void **) &qtheora_encode_tables},
        {"theora_clear", (void **) &qtheora_clear},
        {"theora_granule_time", (void **) &qtheora_granule_time},
+       {"theora_control", (void **) &qtheora_control},
        {NULL, NULL}
 };
 
 static dllhandle_t og_dll = NULL, vo_dll = NULL, ve_dll = NULL, th_dll = NULL;
 
-qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
+qboolean SCR_CaptureVideo_Ogg_OpenLibrary(void)
 {
        const char* dllnames_og [] =
        {
-#if defined(WIN64)
-               "libogg64.dll",
-#elif defined(WIN32)
+#if defined(WIN32)
+               "libogg-0.dll",
                "libogg.dll",
                "ogg.dll",
 #elif defined(MACOSX)
@@ -536,9 +541,8 @@ qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
        };
        const char* dllnames_vo [] =
        {
-#if defined(WIN64)
-               "libvorbis64.dll",
-#elif defined(WIN32)
+#if defined(WIN32)
+               "libvorbis-0.dll",
                "libvorbis.dll",
                "vorbis.dll",
 #elif defined(MACOSX)
@@ -551,9 +555,8 @@ qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
        };
        const char* dllnames_ve [] =
        {
-#if defined(WIN64)
-               "libvorbisenc64.dll",
-#elif defined(WIN32)
+#if defined(WIN32)
+               "libvorbisenc-2.dll",
                "libvorbisenc.dll",
                "vorbisenc.dll",
 #elif defined(MACOSX)
@@ -566,9 +569,8 @@ qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
        };
        const char* dllnames_th [] =
        {
-#if defined(WIN64)
-               "libtheora64.dll",
-#elif defined(WIN32)
+#if defined(WIN32)
+               "libtheora-0.dll",
                "libtheora.dll",
                "theora.dll",
 #elif defined(MACOSX)
@@ -590,10 +592,11 @@ qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
                Sys_LoadLibrary (dllnames_ve, &ve_dll, vorbisencfuncs);
 }
 
-void SCR_CaptureVideo_Ogg_Init()
+void SCR_CaptureVideo_Ogg_Init(void)
 {
        SCR_CaptureVideo_Ogg_OpenLibrary();
 
+       Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_vp3compat);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_quality);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_bitrate);
        Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier);
@@ -604,12 +607,12 @@ void SCR_CaptureVideo_Ogg_Init()
        Cvar_RegisterVariable(&cl_capturevideo_ogg_vorbis_quality);
 }
 
-qboolean SCR_CaptureVideo_Ogg_Available()
+qboolean SCR_CaptureVideo_Ogg_Available(void)
 {
        return og_dll && th_dll && vo_dll && ve_dll;
 }
 
-void SCR_CaptureVideo_Ogg_CloseDLL()
+void SCR_CaptureVideo_Ogg_CloseDLL(void)
 {
        Sys_UnloadLibrary (&ve_dll);
        Sys_UnloadLibrary (&vo_dll);
@@ -650,7 +653,7 @@ typedef struct capturevideostate_ogg_formatspecific_s
 capturevideostate_ogg_formatspecific_t;
 #define LOAD_FORMATSPECIFIC_OGG() capturevideostate_ogg_formatspecific_t *format = (capturevideostate_ogg_formatspecific_t *) cls.capturevideo.formatspecific
 
-static void SCR_CaptureVideo_Ogg_Interleave()
+static void SCR_CaptureVideo_Ogg_Interleave(void)
 {
        LOAD_FORMATSPECIFIC_OGG();
        ogg_page pg;
@@ -708,7 +711,7 @@ static void SCR_CaptureVideo_Ogg_Interleave()
        }
 }
 
-static void SCR_CaptureVideo_Ogg_FlushInterleaving()
+static void SCR_CaptureVideo_Ogg_FlushInterleaving(void)
 {
        LOAD_FORMATSPECIFIC_OGG();
 
@@ -726,7 +729,7 @@ static void SCR_CaptureVideo_Ogg_FlushInterleaving()
        }
 }
 
-static void SCR_CaptureVideo_Ogg_EndVideo()
+static void SCR_CaptureVideo_Ogg_EndVideo(void)
 {
        LOAD_FORMATSPECIFIC_OGG();
        ogg_page pg;
@@ -819,7 +822,7 @@ static void SCR_CaptureVideo_Ogg_EndVideo()
        cls.capturevideo.videofile = NULL;
 }
 
-static void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV()
+static void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV(void)
 {
        LOAD_FORMATSPECIFIC_OGG();
        yuv_buffer *yuv;
@@ -932,7 +935,7 @@ static void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintb
        SCR_CaptureVideo_Ogg_Interleave();
 }
 
-void SCR_CaptureVideo_Ogg_BeginVideo()
+void SCR_CaptureVideo_Ogg_BeginVideo(void)
 {
        cls.capturevideo.format = CAPTUREVIDEOFORMAT_OGG_VORBIS_THEORA;
        cls.capturevideo.formatextension = "ogv";
@@ -949,6 +952,7 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                theora_comment tc;
                vorbis_comment vc;
                theora_info ti;
+               int vp3compat;
 
                format->serial1 = rand();
                qogg_stream_init(&format->to, format->serial1);
@@ -981,13 +985,13 @@ 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
 
-               FindFraction(cls.capturevideo.framerate, &num, &denom, 1001);
+               FindFraction(cls.capturevideo.framerate / cls.capturevideo.framestep, &num, &denom, 1001);
                ti.fps_numerator = num;
                ti.fps_denominator = denom;
 
@@ -1006,32 +1010,24 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
 
                if(ti.target_bitrate <= 0)
                {
-                       if(ti.quality < 0)
-                       {
-                               ti.target_bitrate = -1;
-                               ti.keyframe_data_target_bitrate = -1;
-                               ti.quality = 63;
-                       }
-                       else
-                       {
-                               ti.target_bitrate = -1;
-                               ti.keyframe_data_target_bitrate = -1;
-                               ti.quality = bound(0, ti.quality, 63);
-                       }
+                       ti.target_bitrate = -1;
+                       ti.keyframe_data_target_bitrate = (unsigned int)-1;
                }
                else
                {
-                       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.quality = -1;
-                       }
-                       else
+                       ti.keyframe_data_target_bitrate = (int) (ti.target_bitrate * max(1, cl_capturevideo_ogg_theora_keyframe_bitrate_multiplier.value));
+
+                       if(ti.target_bitrate < 45000 || ti.target_bitrate > 2000000)
+                               Con_DPrintf("WARNING: requesting an odd bitrate for theora (sensible values range from 45 to 2000 kbps)\n");
+               }
+
+               if(ti.quality < 0 || ti.quality > 63)
+               {
+                       ti.quality = 63;
+                       if(ti.target_bitrate <= 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.quality = -1;
+                               ti.target_bitrate = 0x7FFFFFFF;
+                               ti.keyframe_data_target_bitrate = 0x7FFFFFFF;
                        }
                }
 
@@ -1048,6 +1044,14 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                qtheora_encode_init(&format->ts, &ti);
                qtheora_info_clear(&ti);
 
+               if(cl_capturevideo_ogg_theora_vp3compat.integer)
+               {
+                       vp3compat = 1;
+                       qtheora_control(&format->ts, TH_ENCCTL_SET_VP3_COMPATIBLE, &vp3compat, sizeof(vp3compat));
+                       if(!vp3compat)
+                               Con_DPrintf("Warning: theora stream is not fully VP3 compatible\n");
+               }
+
                // vorbis?
                if(cls.capturevideo.soundrate)
                {