]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cap_ogg.c
qdefs: Check _MSC_VER instead of incorrect MSVC
[xonotic/darkplaces.git] / cap_ogg.c
index 71d05cd071b0eaea58bf3fea9e0d93ef5594bcb4..6218e8ad5fd71d51040d5a90ace806fa4d46becc 100644 (file)
--- a/cap_ogg.c
+++ b/cap_ogg.c
@@ -1,6 +1,3 @@
-#ifndef _MSC_VER
-#include <stdint.h>
-#endif
 #include <sys/types.h>
 
 #include "quakedef.h"
@@ -8,30 +5,16 @@
 #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_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)"};
-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"};
-
-// ogg.h stuff
-#ifdef _MSC_VER
-typedef __int16 ogg_int16_t;
-typedef unsigned __int16 ogg_uint16_t;
-typedef __int32 ogg_int32_t;
-typedef unsigned __int32 ogg_uint32_t;
-typedef __int64 ogg_int64_t;
-#else
-typedef int16_t ogg_int16_t;
-typedef uint16_t ogg_uint16_t;
-typedef int32_t ogg_int32_t;
-typedef uint32_t ogg_uint32_t;
-typedef int64_t ogg_int64_t;
-#endif
+static cvar_t cl_capturevideo_ogg_theora_vp3compat = {CF_CLIENT | CF_ARCHIVE, "cl_capturevideo_ogg_theora_vp3compat", "1", "make VP3 compatible theora streams"};
+static cvar_t cl_capturevideo_ogg_theora_quality = {CF_CLIENT | CF_ARCHIVE, "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 = {CF_CLIENT | CF_ARCHIVE, "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 = {CF_CLIENT | CF_ARCHIVE, "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 = {CF_CLIENT | CF_ARCHIVE, "cl_capturevideo_ogg_theora_keyframe_maxinterval", "64", "maximum keyframe interval (1 to 1000)"};
+static cvar_t cl_capturevideo_ogg_theora_keyframe_mininterval = {CF_CLIENT | CF_ARCHIVE, "cl_capturevideo_ogg_theora_keyframe_mininterval", "8", "minimum keyframe interval (1 to 1000)"};
+static cvar_t cl_capturevideo_ogg_theora_keyframe_auto_threshold = {CF_CLIENT | CF_ARCHIVE, "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 = {CF_CLIENT | CF_ARCHIVE, "cl_capturevideo_ogg_theora_noise_sensitivity", "1", "video noise sensitivity (0 to 6); lower is better"};
+static cvar_t cl_capturevideo_ogg_theora_sharpness = {CF_CLIENT | CF_ARCHIVE, "cl_capturevideo_ogg_theora_sharpness", "0", "sharpness (0 to 2); lower is sharper"};
+static cvar_t cl_capturevideo_ogg_vorbis_quality = {CF_CLIENT | CF_ARCHIVE, "cl_capturevideo_ogg_vorbis_quality", "3", "audio quality (-1 to 10); higher is better"};
 
 typedef struct {
   long endbyte;
@@ -62,7 +45,7 @@ typedef struct {
 
 
   int     *lacing_vals;      /* The values that will go to the segment table */
-  ogg_int64_t *granule_vals; /* granulepos values for headers. Not compact
+  int64_t *granule_vals; /* granulepos values for headers. Not compact
                                this way, but it is simple coupled to the
                                lacing fifo */
   long    lacing_storage;
@@ -79,12 +62,12 @@ typedef struct {
                              of a logical bitstream */
   long    serialno;
   long    pageno;
-  ogg_int64_t  packetno;      /* sequence number for decode; the framing
+  int64_t  packetno;      /* sequence number for decode; the framing
                              knows where there's a hole in the data,
                              but we need coupling so that the codec
                              (which is in a seperate abstraction
                              layer) also knows about the gap */
-  ogg_int64_t   granulepos;
+  int64_t   granulepos;
 
 } ogg_stream_state;
 
@@ -97,9 +80,9 @@ typedef struct {
   long  b_o_s;
   long  e_o_s;
 
-  ogg_int64_t  granulepos;
+  int64_t  granulepos;
   
-  ogg_int64_t  packetno;     /* sequence number for decode; the framing
+  int64_t  packetno;     /* sequence number for decode; the framing
                                knows where there's a hole in the data,
                                but we need coupling so that the codec
                                (which is in a seperate abstraction
@@ -127,7 +110,7 @@ static int      (*qogg_stream_flush) (ogg_stream_state *os, ogg_page *og);
 
 static int      (*qogg_stream_init) (ogg_stream_state *os,int serialno);
 static int      (*qogg_stream_clear) (ogg_stream_state *os);
-static ogg_int64_t  (*qogg_page_granulepos) (ogg_page *og);
+static int64_t  (*qogg_page_granulepos) (ogg_page *og);
 
 // end of ogg.h stuff
 
@@ -181,13 +164,13 @@ typedef struct vorbis_dsp_state{
   long nW;
   long centerW;
 
-  ogg_int64_t granulepos;
-  ogg_int64_t sequence;
+  int64_t granulepos;
+  int64_t sequence;
 
-  ogg_int64_t glue_bits;
-  ogg_int64_t time_bits;
-  ogg_int64_t floor_bits;
-  ogg_int64_t res_bits;
+  int64_t glue_bits;
+  int64_t time_bits;
+  int64_t floor_bits;
+  int64_t res_bits;
 
   void       *backend_state;
 } vorbis_dsp_state;
@@ -204,8 +187,8 @@ typedef struct vorbis_block{
   int   mode;
 
   int         eofflag;
-  ogg_int64_t granulepos;
-  ogg_int64_t sequence;
+  int64_t granulepos;
+  int64_t sequence;
   vorbis_dsp_state *vd; /* For read-only access of configuration */
 
   /* local storage to avoid remallocing; it's up to the mapping to
@@ -278,7 +261,7 @@ static int      (*qvorbis_block_init) (vorbis_dsp_state *v, vorbis_block *vb);
 static int      (*qvorbis_block_clear) (vorbis_block *vb);
 static void     (*qvorbis_dsp_clear) (vorbis_dsp_state *v);
 static double   (*qvorbis_granule_time) (vorbis_dsp_state *v,
-                                   ogg_int64_t granulepos);
+                                   int64_t granulepos);
 
 /* Vorbis PRIMITIVES: analysis/DSP layer ****************************/
 
@@ -310,6 +293,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 +331,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.
@@ -369,16 +355,16 @@ typedef enum {
  * other paramters and good default settings for the encoder parameters.
  */
 typedef struct {
-  ogg_uint32_t  width;      /**< encoded frame width  */
-  ogg_uint32_t  height;     /**< encoded frame height */
-  ogg_uint32_t  frame_width;    /**< display frame width  */
-  ogg_uint32_t  frame_height;   /**< display frame height */
-  ogg_uint32_t  offset_x;   /**< horizontal offset of the displayed frame */
-  ogg_uint32_t  offset_y;   /**< vertical offset of the displayed frame */
-  ogg_uint32_t  fps_numerator;      /**< frame rate numerator **/
-  ogg_uint32_t  fps_denominator;    /**< frame rate denominator **/
-  ogg_uint32_t  aspect_numerator;   /**< pixel aspect ratio numerator */
-  ogg_uint32_t  aspect_denominator; /**< pixel aspect ratio denominator */
+  uint32_t  width;      /**< encoded frame width  */
+  uint32_t  height;     /**< encoded frame height */
+  uint32_t  frame_width;    /**< display frame width  */
+  uint32_t  frame_height;   /**< display frame height */
+  uint32_t  offset_x;   /**< horizontal offset of the displayed frame */
+  uint32_t  offset_y;   /**< vertical offset of the displayed frame */
+  uint32_t  fps_numerator;      /**< frame rate numerator **/
+  uint32_t  fps_denominator;    /**< frame rate denominator **/
+  uint32_t  aspect_numerator;   /**< pixel aspect ratio numerator */
+  uint32_t  aspect_denominator; /**< pixel aspect ratio denominator */
   theora_colorspace colorspace;     /**< colorspace */
   int           target_bitrate;     /**< nominal bitrate in bits per second */
   int           quality;  /**< Nominal quality setting, 0-63 */
@@ -394,14 +380,14 @@ typedef struct {
   /* encode only */
   int           dropframes_p;
   int           keyframe_auto_p;
-  ogg_uint32_t  keyframe_frequency;
-  ogg_uint32_t  keyframe_frequency_force;  /* also used for decode init to
+  uint32_t  keyframe_frequency;
+  uint32_t  keyframe_frequency_force;  /* also used for decode init to
                                               get granpos shift correct */
-  ogg_uint32_t  keyframe_data_target_bitrate;
-  ogg_int32_t   keyframe_auto_threshold;
-  ogg_uint32_t  keyframe_mindistance;
-  ogg_int32_t   noise_sensitivity;
-  ogg_int32_t   sharpness;
+  uint32_t  keyframe_data_target_bitrate;
+  int32_t   keyframe_auto_threshold;
+  uint32_t  keyframe_mindistance;
+  int32_t   noise_sensitivity;
+  int32_t   sharpness;
 
   theora_pixelformat pixelformat;   /**< chroma subsampling mode to expect */
 
@@ -411,7 +397,7 @@ typedef struct {
  */
 typedef struct{
   theora_info *i;
-  ogg_int64_t granulepos;
+  int64_t granulepos;
 
   void *internal_encode;
   void *internal_decode;
@@ -456,7 +442,8 @@ static void (*qtheora_info_clear) (theora_info *c);
 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 double (*qtheora_granule_time) (theora_state *th,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 +499,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()
+static qbool 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 +523,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 +537,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 +551,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)
@@ -581,19 +565,20 @@ qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
        };
 
        return
-               Sys_LoadLibrary (dllnames_og, &og_dll, oggfuncs)
+               Sys_LoadDependency (dllnames_og, &og_dll, oggfuncs)
                &&
-               Sys_LoadLibrary (dllnames_th, &th_dll, theorafuncs)
+               Sys_LoadDependency (dllnames_th, &th_dll, theorafuncs)
                &&
-               Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs)
+               Sys_LoadDependency (dllnames_vo, &vo_dll, vorbisfuncs)
                &&
-               Sys_LoadLibrary (dllnames_ve, &ve_dll, vorbisencfuncs);
+               Sys_LoadDependency (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,17 +589,17 @@ void SCR_CaptureVideo_Ogg_Init()
        Cvar_RegisterVariable(&cl_capturevideo_ogg_vorbis_quality);
 }
 
-qboolean SCR_CaptureVideo_Ogg_Available()
+qbool 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);
-       Sys_UnloadLibrary (&th_dll);
-       Sys_UnloadLibrary (&og_dll);
+       Sys_FreeLibrary (&ve_dll);
+       Sys_FreeLibrary (&vo_dll);
+       Sys_FreeLibrary (&th_dll);
+       Sys_FreeLibrary (&og_dll);
 }
 
 // this struct should not be needed
@@ -650,7 +635,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;
@@ -674,7 +659,7 @@ static void SCR_CaptureVideo_Ogg_Interleave()
                                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");
+                                       Sys_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);
                        }
@@ -684,7 +669,7 @@ static void SCR_CaptureVideo_Ogg_Interleave()
                                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");
+                                       Sys_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);
                        }
@@ -708,7 +693,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 +711,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;
@@ -779,7 +764,7 @@ static void SCR_CaptureVideo_Ogg_EndVideo()
        while (1) {
                int result = qogg_stream_flush (&format->to, &pg);
                if (result < 0)
-                       fprintf (stderr, "Internal Ogg library error.\n"); // TODO Host_Error
+                       fprintf (stderr, "Internal Ogg library error.\n"); // TODO Sys_Error
                if (result <= 0)
                        break;
                FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
@@ -791,7 +776,7 @@ static void SCR_CaptureVideo_Ogg_EndVideo()
                while (1) {
                        int result = qogg_stream_flush (&format->vo, &pg);
                        if (result < 0)
-                               fprintf (stderr, "Internal Ogg library error.\n"); // TODO Host_Error
+                               fprintf (stderr, "Internal Ogg library error.\n"); // TODO Sys_Error
                        if (result <= 0)
                                break;
                        FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
@@ -819,13 +804,13 @@ 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;
        int x, y;
        int blockr, blockg, blockb;
-       unsigned char *b = cls.capturevideo.outbuffer;
+       unsigned char *b;
        int w = cls.capturevideo.width;
        int h = cls.capturevideo.height;
        int inpitch = w*4;
@@ -844,7 +829,7 @@ static void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV()
                        b += 4;
                }
 
-               if((y & 1) == 0)
+               if ((y & 1) == 0 && y/2 < h/2) // if h is odd, this skips the last row
                {
                        for(b = cls.capturevideo.outbuffer + (h-2-y)*w*4, x = 0; x < w/2; ++x)
                        {
@@ -889,6 +874,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();
@@ -896,14 +894,16 @@ 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)
+       vorbis_buffer = qvorbis_analysis_buffer(&format->vd, (int)length);
+       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];
        }
-       qvorbis_analysis_wrote(&format->vd, length);
+       qvorbis_analysis_wrote(&format->vd, (int)length);
 
        while(qvorbis_analysis_blockout(&format->vd, &format->vb) == 1)
        {
@@ -917,11 +917,12 @@ 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)
 {
+       char vabuf[1024];
        cls.capturevideo.format = CAPTUREVIDEOFORMAT_OGG_VORBIS_THEORA;
        cls.capturevideo.formatextension = "ogv";
-       cls.capturevideo.videofile = FS_OpenRealFile(va("%s.%s", cls.capturevideo.basename, cls.capturevideo.formatextension), "wb", false);
+       cls.capturevideo.videofile = FS_OpenRealFile(va(vabuf, sizeof(vabuf), "%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;
@@ -934,6 +935,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);
@@ -966,13 +968,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;
 
@@ -991,32 +993,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;
                        }
                }
 
@@ -1033,6 +1027,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)
                {
@@ -1079,7 +1081,7 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                {
                        int result = qogg_stream_flush (&format->to, &pg);
                        if (result < 0)
-                               fprintf (stderr, "Internal Ogg library error.\n"); // TODO Host_Error
+                               fprintf (stderr, "Internal Ogg library error.\n"); // TODO Sys_Error
                        if (result <= 0)
                                break;
                        FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
@@ -1091,7 +1093,7 @@ void SCR_CaptureVideo_Ogg_BeginVideo()
                {
                        int result = qogg_stream_flush (&format->vo, &pg);
                        if (result < 0)
-                               fprintf (stderr, "Internal Ogg library error.\n"); // TODO Host_Error
+                               fprintf (stderr, "Internal Ogg library error.\n"); // TODO Sys_Error
                        if (result <= 0)
                                break;
                        FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);