]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cap_ogg.c
capturevideo refactoring, making AVI also "just a module" for it
[xonotic/darkplaces.git] / cap_ogg.c
index 410298d0bd6a5f334eba1b45b1b14b1a339900d0..c1030a02597dec24b98a39c4566bfcd5455f62fa 100644 (file)
--- a/cap_ogg.c
+++ b/cap_ogg.c
@@ -4,6 +4,17 @@
 #include "client.h"
 #include "cap_ogg.h"
 
+// video capture cvars
+static cvar_t cl_capturevideo_ogg_theora_quality = {CVAR_SAVE, "cl_capturevideo_ogg_theora_quality", "16", "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_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
 typedef int16_t ogg_int16_t;
 typedef u_int16_t ogg_uint16_t;
@@ -564,6 +575,15 @@ qboolean SCR_CaptureVideo_Ogg_OpenLibrary()
 void SCR_CaptureVideo_Ogg_Init()
 {
        SCR_CaptureVideo_Ogg_OpenLibrary();
+
+       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_auto_threshold);
+       Cvar_RegisterVariable(&cl_capturevideo_ogg_theora_noise_sensitivity);
+       Cvar_RegisterVariable(&cl_capturevideo_ogg_vorbis_quality);
 }
 
 qboolean SCR_CaptureVideo_Ogg_Available()
@@ -583,159 +603,19 @@ typedef struct capturevideostate_ogg_formatspecific_s
 {
        ogg_stream_state to, vo;
        int serial1, serial2;
-       theora_info ti;
        theora_state ts;
-       vorbis_info vi;
        vorbis_dsp_state vd;
        vorbis_block vb;
+       vorbis_info vi;
        yuv_buffer yuv;
        int channels;
 }
 capturevideostate_ogg_formatspecific_t;
-#define LOAD_FORMATSPECIFIC() capturevideostate_ogg_formatspecific_t *format = (capturevideostate_ogg_formatspecific_t *) cls.capturevideo.formatspecific
-
-void SCR_CaptureVideo_Ogg_Begin()
-{
-       cls.capturevideo.videofile = FS_OpenRealFile(va("%s.ogv", cls.capturevideo.basename), "wb", false);
-       cls.capturevideo.formatspecific = Mem_Alloc(tempmempool, sizeof(capturevideostate_ogg_formatspecific_t));
-       {
-               LOAD_FORMATSPECIFIC();
-               int num, denom;
-               ogg_page pg;
-               ogg_packet pt, pt2, pt3;
-               theora_comment tc;
-               vorbis_comment vc;
-
-               format->serial1 = rand();
-               qogg_stream_init(&format->to, format->serial1);
-
-               if(cls.capturevideo.soundrate)
-               {
-                       do
-                       {
-                               format->serial2 = rand();
-                       }
-                       while(format->serial1 == format->serial2);
-                       qogg_stream_init(&format->vo, format->serial2);
-               }
-
-               qtheora_info_init(&format->ti);
-               format->ti.frame_width = cls.capturevideo.width;
-               format->ti.frame_height = cls.capturevideo.height;
-               format->ti.width = (format->ti.frame_width + 15) & ~15;
-               format->ti.height = (format->ti.frame_height + 15) & ~15;
-               format->ti.offset_x = ((format->ti.width - format->ti.frame_width) / 2) & ~1;
-               format->ti.offset_y = ((format->ti.height - format->ti.frame_height) / 2) & ~1;
-
-               format->yuv.y_width = format->ti.width;
-               format->yuv.y_height = format->ti.height;
-               format->yuv.y_stride = format->ti.width;
-
-               format->yuv.uv_width = format->ti.width / 2;
-               format->yuv.uv_height = format->ti.height / 2;
-               format->yuv.uv_stride = format->ti.width / 2;
-
-               format->yuv.y = Mem_Alloc(tempmempool, format->yuv.y_stride * format->yuv.y_height);
-               format->yuv.u = Mem_Alloc(tempmempool, format->yuv.uv_stride * format->yuv.uv_height);
-               format->yuv.v = Mem_Alloc(tempmempool, format->yuv.uv_stride * format->yuv.uv_height);
-
-               FindFraction(cls.capturevideo.framerate, &num, &denom, 1001);
-               format->ti.fps_numerator = num;
-               format->ti.fps_denominator = denom;
-
-               FindFraction(1 / vid_pixelheight.value, &num, &denom, 1000);
-               format->ti.aspect_numerator = num;
-               format->ti.aspect_denominator = denom;
-
-               format->ti.colorspace = OC_CS_UNSPECIFIED;
-               format->ti.pixelformat = OC_PF_420;
-
-               format->ti.target_bitrate = -1;
-               format->ti.quality = 63; // TODO find good values here later
-               format->ti.quick_p = false;
-
-               format->ti.dropframes_p = false;
-               format->ti.keyframe_auto_p = false;
-               format->ti.keyframe_frequency = 64;
-               format->ti.keyframe_frequency_force = 64; // TODO
-               format->ti.keyframe_data_target_bitrate = -1;
-               format->ti.keyframe_auto_threshold = 80;
-               format->ti.keyframe_mindistance = 8;
-               format->ti.noise_sensitivity = 1; // TODO
-               format->ti.sharpness = 0;
-
-               qtheora_encode_init(&format->ts, &format->ti);
-
-               // vorbis?
-               if(cls.capturevideo.soundrate)
-               {
-                       qvorbis_info_init(&format->vi);
-                       qvorbis_encode_init_vbr(&format->vi, cls.capturevideo.soundchannels, cls.capturevideo.soundrate, 9);
-                       qvorbis_comment_init(&vc);
-                       qvorbis_analysis_init(&format->vd, &format->vi);
-                       qvorbis_block_init(&format->vd, &format->vb);
-               }
-
-               qtheora_comment_init(&tc);
-
-               /* create the remaining theora headers */
-               qtheora_encode_header(&format->ts, &pt);
-               qogg_stream_packetin(&format->to, &pt);
-               if (qogg_stream_pageout (&format->to, &pg) != 1)
-                       fprintf (stderr, "Internal Ogg library error.\n");
-               FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
-               FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
-
-               qtheora_encode_comment(&tc, &pt);
-               qogg_stream_packetin(&format->to, &pt);
-               qtheora_encode_tables(&format->ts, &pt);
-               qogg_stream_packetin (&format->to, &pt);
-
-               qtheora_comment_clear(&tc);
-
-               if(cls.capturevideo.soundrate)
-               {
-                       qvorbis_analysis_headerout(&format->vd, &vc, &pt, &pt2, &pt3);
-                       qogg_stream_packetin(&format->vo, &pt);
-                       if (qogg_stream_pageout (&format->vo, &pg) != 1)
-                               fprintf (stderr, "Internal Ogg library error.\n");
-                       FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
-                       FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
-
-                       qogg_stream_packetin(&format->vo, &pt2);
-                       qogg_stream_packetin(&format->vo, &pt3);
-
-                       qvorbis_comment_clear(&vc);
-               }
-
-               for(;;)
-               {
-                       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)
-               for(;;)
-               {
-                       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);
-               }
-       }
-}
+#define LOAD_FORMATSPECIFIC_OGG() capturevideostate_ogg_formatspecific_t *format = (capturevideostate_ogg_formatspecific_t *) cls.capturevideo.formatspecific
 
-void SCR_CaptureVideo_Ogg_EndVideo()
+static void SCR_CaptureVideo_Ogg_EndVideo()
 {
-       LOAD_FORMATSPECIFIC();
+       LOAD_FORMATSPECIFIC_OGG();
        ogg_page pg;
        ogg_packet pt;
 
@@ -756,7 +636,7 @@ void SCR_CaptureVideo_Ogg_EndVideo()
                }
        }
 
-       if(qogg_stream_pageout(&format->to, &pg) > 0)
+       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);
@@ -764,7 +644,7 @@ void SCR_CaptureVideo_Ogg_EndVideo()
 
        if(cls.capturevideo.soundrate)
        {
-               if(qogg_stream_pageout(&format->vo, &pg) > 0)
+               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);
@@ -796,22 +676,24 @@ void SCR_CaptureVideo_Ogg_EndVideo()
                qogg_stream_clear(&format->vo);
                qvorbis_block_clear(&format->vb);
                qvorbis_dsp_clear(&format->vd);
-               qvorbis_info_clear(&format->vi);
        }
 
        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);
 
-       // cl_screen.c does this
-       // FS_Close(cls.capturevideo.videofile);
-       // cls.capturevideo.videofile = NULL;
+       FS_Close(cls.capturevideo.videofile);
+       cls.capturevideo.videofile = NULL;
 }
 
-void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV()
+static void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV()
 {
-       LOAD_FORMATSPECIFIC();
+       LOAD_FORMATSPECIFIC_OGG();
        int x, y;
        int blockr, blockg, blockb;
        unsigned char *b = cls.capturevideo.outbuffer;
@@ -848,29 +730,33 @@ void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV()
        }
 }
 
-void SCR_CaptureVideo_Ogg_VideoFrame()
+static void SCR_CaptureVideo_Ogg_VideoFrames(int num)
 {
-       LOAD_FORMATSPECIFIC();
+       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();
-       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)
+       while(num-- > 0)
        {
-               FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
-               FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+               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);
+               }
        }
 }
 
-void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length)
+static void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length)
 {
-       LOAD_FORMATSPECIFIC();
+       LOAD_FORMATSPECIFIC_OGG();
        float **vorbis_buffer;
        size_t i;
        int j;
@@ -900,3 +786,180 @@ void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer,
                FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
        }
 }
+
+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.endvideo = SCR_CaptureVideo_Ogg_EndVideo;
+       cls.capturevideo.videoframes = SCR_CaptureVideo_Ogg_VideoFrames;
+       cls.capturevideo.soundframe = SCR_CaptureVideo_Ogg_SoundFrame;
+       cls.capturevideo.formatspecific = Mem_Alloc(tempmempool, sizeof(capturevideostate_ogg_formatspecific_t));
+       {
+               LOAD_FORMATSPECIFIC_OGG();
+               int num, denom;
+               ogg_page pg;
+               ogg_packet pt, pt2, pt3;
+               theora_comment tc;
+               vorbis_comment vc;
+               theora_info ti;
+
+               format->serial1 = rand();
+               qogg_stream_init(&format->to, format->serial1);
+
+               if(cls.capturevideo.soundrate)
+               {
+                       do
+                       {
+                               format->serial2 = rand();
+                       }
+                       while(format->serial1 == format->serial2);
+                       qogg_stream_init(&format->vo, format->serial2);
+               }
+
+               qtheora_info_init(&ti);
+               ti.frame_width = cls.capturevideo.width;
+               ti.frame_height = cls.capturevideo.height;
+               ti.width = (ti.frame_width + 15) & ~15;
+               ti.height = (ti.frame_height + 15) & ~15;
+               //ti.offset_x = ((ti.width - ti.frame_width) / 2) & ~1;
+               //ti.offset_y = ((ti.height - ti.frame_height) / 2) & ~1;
+
+               format->yuv.y_width = ti.width;
+               format->yuv.y_height = ti.height;
+               format->yuv.y_stride = ti.width;
+
+               format->yuv.uv_width = ti.width / 2;
+               format->yuv.uv_height = ti.height / 2;
+               format->yuv.uv_stride = ti.width / 2;
+
+               format->yuv.y = Mem_Alloc(tempmempool, format->yuv.y_stride * format->yuv.y_height);
+               format->yuv.u = Mem_Alloc(tempmempool, format->yuv.uv_stride * format->yuv.uv_height);
+               format->yuv.v = Mem_Alloc(tempmempool, format->yuv.uv_stride * format->yuv.uv_height);
+
+               FindFraction(cls.capturevideo.framerate, &num, &denom, 1001);
+               ti.fps_numerator = num;
+               ti.fps_denominator = denom;
+
+               FindFraction(1 / vid_pixelheight.value, &num, &denom, 1000);
+               ti.aspect_numerator = num;
+               ti.aspect_denominator = denom;
+
+               ti.colorspace = OC_CS_UNSPECIFIED;
+               ti.pixelformat = OC_PF_420;
+
+               ti.quick_p = true; // http://mlblog.osdir.com/multimedia.ogg.theora.general/2004-07/index.shtml
+               ti.dropframes_p = false;
+
+               ti.target_bitrate = cl_capturevideo_ogg_theora_bitrate.integer * 1000;
+               ti.quality = cl_capturevideo_ogg_theora_quality.integer;
+
+               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);
+                       }
+               }
+               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.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.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);
+               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);
+
+               qtheora_encode_init(&format->ts, &ti);
+               qtheora_info_clear(&ti);
+
+               // vorbis?
+               if(cls.capturevideo.soundrate)
+               {
+                       qvorbis_info_init(&format->vi);
+                       qvorbis_encode_init_vbr(&format->vi, cls.capturevideo.soundchannels, cls.capturevideo.soundrate, bound(-1, cl_capturevideo_ogg_vorbis_quality.value, 10) * 0.099);
+                       qvorbis_comment_init(&vc);
+                       qvorbis_analysis_init(&format->vd, &format->vi);
+                       qvorbis_block_init(&format->vd, &format->vb);
+               }
+
+               qtheora_comment_init(&tc);
+
+               /* create the remaining theora headers */
+               qtheora_encode_header(&format->ts, &pt);
+               qogg_stream_packetin(&format->to, &pt);
+               if (qogg_stream_pageout (&format->to, &pg) != 1)
+                       fprintf (stderr, "Internal Ogg library error.\n");
+               FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+               FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+
+               qtheora_encode_comment(&tc, &pt);
+               qogg_stream_packetin(&format->to, &pt);
+               qtheora_encode_tables(&format->ts, &pt);
+               qogg_stream_packetin (&format->to, &pt);
+
+               qtheora_comment_clear(&tc);
+
+               if(cls.capturevideo.soundrate)
+               {
+                       qvorbis_analysis_headerout(&format->vd, &vc, &pt, &pt2, &pt3);
+                       qogg_stream_packetin(&format->vo, &pt);
+                       if (qogg_stream_pageout (&format->vo, &pg) != 1)
+                               fprintf (stderr, "Internal Ogg library error.\n");
+                       FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len);
+                       FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len);
+
+                       qogg_stream_packetin(&format->vo, &pt2);
+                       qogg_stream_packetin(&format->vo, &pt3);
+
+                       qvorbis_comment_clear(&vc);
+               }
+
+               for(;;)
+               {
+                       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)
+               for(;;)
+               {
+                       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);
+               }
+       }
+}