X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=cap_ogg.c;h=e9abc7ae573fa143a1be388d64f3a64466451291;hb=212a93f6f5c06d50d331d61e1792d12f11ca1595;hp=bed8630c05c919ed6114d71e328d04694e75aab3;hpb=4386519b514b8e53c6b3ecc081cad2f6340bdace;p=xonotic%2Fdarkplaces.git diff --git a/cap_ogg.c b/cap_ogg.c index bed8630c..e9abc7ae 100644 --- a/cap_ogg.c +++ b/cap_ogg.c @@ -1,3 +1,6 @@ +#ifndef _MSC_VER +#include +#endif #include #include "quakedef.h" @@ -5,22 +8,31 @@ #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_quality", "-1", "video bitrate (45000 to 2000000 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_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", "5", "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 +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 u_int16_t ogg_uint16_t; +typedef uint16_t ogg_uint16_t; typedef int32_t ogg_int32_t; -typedef u_int32_t ogg_uint32_t; +typedef uint32_t ogg_uint32_t; typedef int64_t ogg_int64_t; +#endif typedef struct { long endbyte; @@ -116,6 +128,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); // end of ogg.h stuff @@ -265,6 +278,8 @@ static void (*qvorbis_comment_clear) (vorbis_comment *vc); 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); /* Vorbis PRIMITIVES: analysis/DSP layer ****************************/ @@ -296,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 */ @@ -331,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. @@ -442,6 +460,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 int (*qtheora_control) (theora_state *th,int req,void *buf,size_t buf_sz); // end of theora.h stuff static dllfunction_t oggfuncs[] = @@ -451,6 +471,7 @@ static dllfunction_t oggfuncs[] = {"ogg_stream_flush", (void **) &qogg_stream_flush}, {"ogg_stream_init", (void **) &qogg_stream_init}, {"ogg_stream_clear", (void **) &qogg_stream_clear}, + {"ogg_page_granulepos", (void **) &qogg_page_granulepos}, {NULL, NULL} }; @@ -478,6 +499,7 @@ static dllfunction_t vorbisfuncs[] = {"vorbis_analysis", (void **) &qvorbis_analysis}, {"vorbis_bitrate_addblock", (void **) &qvorbis_bitrate_addblock}, {"vorbis_bitrate_flushpacket", (void **) &qvorbis_bitrate_flushpacket}, + {"vorbis_granule_time", (void **) &qvorbis_granule_time}, {NULL, NULL} }; @@ -494,18 +516,19 @@ static dllfunction_t theorafuncs[] = {"theora_encode_comment", (void **) &qtheora_encode_comment}, {"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 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) @@ -518,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) @@ -533,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) @@ -548,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) @@ -572,26 +592,27 @@ 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); - 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); } -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); @@ -599,6 +620,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; @@ -606,197 +642,112 @@ typedef struct capturevideostate_ogg_formatspecific_s theora_state ts; vorbis_dsp_state vd; vorbis_block vb; - yuv_buffer yuv; + vorbis_info vi; + yuv_buffer yuv[2]; + int yuvi; + int lastnum; int channels; + + allocatedoggpage_t videopage, audiopage; } capturevideostate_ogg_formatspecific_t; -#define LOAD_FORMATSPECIFIC() capturevideostate_ogg_formatspecific_t *format = (capturevideostate_ogg_formatspecific_t *) cls.capturevideo.formatspecific +#define LOAD_FORMATSPECIFIC_OGG() capturevideostate_ogg_formatspecific_t *format = (capturevideostate_ogg_formatspecific_t *) cls.capturevideo.formatspecific -void SCR_CaptureVideo_Ogg_Begin() +static void SCR_CaptureVideo_Ogg_Interleave(void) { - 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; - theora_info ti; - vorbis_info vi; - - format->serial1 = rand(); - qogg_stream_init(&format->to, format->serial1); + LOAD_FORMATSPECIFIC_OGG(); + ogg_page pg; - if(cls.capturevideo.soundrate) + if(!cls.capturevideo.soundrate) + { + while(qogg_stream_pageout(&format->to, &pg) > 0) { - do - { - format->serial2 = rand(); - } - while(format->serial1 == format->serial2); - qogg_stream_init(&format->vo, format->serial2); + FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len); + FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len); } + return; + } - 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; - ti.quality = cl_capturevideo_ogg_theora_quality.integer; - - if(ti.target_bitrate <= 0) - { - if(ti.quality < 0) + for(;;) + { + // first: make sure we have a page of both types + if(!format->videopage.len) + if(qogg_stream_pageout(&format->to, &pg) > 0) { - ti.target_bitrate = -1; - ti.keyframe_data_target_bitrate = -1; - ti.quality = 63; + 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)) + 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); } - else + if(!format->audiopage.len) + if(qogg_stream_pageout(&format->vo, &pg) > 0) { - ti.target_bitrate = -1; - ti.keyframe_data_target_bitrate = -1; - ti.quality = bound(0, ti.quality, 63); + 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)) + 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); } - } - else + + if(format->videopage.len && format->audiopage.len) { - if(ti.quality < 0) + // output the page that ends first + if(format->videopage.time < format->audiopage.time) { - 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; + FS_Write(cls.capturevideo.videofile, format->videopage.data, format->videopage.len); + format->videopage.len = 0; } 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; + FS_Write(cls.capturevideo.videofile, format->audiopage.data, format->audiopage.len); + format->audiopage.len = 0; } } + else + break; + } +} - 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(&vi); - qvorbis_encode_init_vbr(&vi, cls.capturevideo.soundchannels, cls.capturevideo.soundrate, bound(-1, cl_capturevideo_ogg_vorbis_quality.value, 10) * 0.1); - qvorbis_comment_init(&vc); - qvorbis_analysis_init(&format->vd, &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); - qvorbis_info_clear(&vi); - } +static void SCR_CaptureVideo_Ogg_FlushInterleaving(void) +{ + LOAD_FORMATSPECIFIC_OGG(); - 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) + if(format->audiopage.len) + { + FS_Write(cls.capturevideo.videofile, format->audiopage.data, format->audiopage.len); + format->audiopage.len = 0; + } - 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); - } + if(format->videopage.len) + { + FS_Write(cls.capturevideo.videofile, format->videopage.data, format->videopage.len); + format->videopage.len = 0; } } -void SCR_CaptureVideo_Ogg_EndVideo() +static void SCR_CaptureVideo_Ogg_EndVideo(void) { - LOAD_FORMATSPECIFIC(); + 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(format->yuvi >= 0) + { + // send the previous (and last) frame + while(format->lastnum-- > 0) + { + qtheora_encode_YUVin(&format->ts, &format->yuv[format->yuvi]); + + while(qtheora_encode_packetout(&format->ts, !format->lastnum, &pt)) + qogg_stream_packetin(&format->to, &pt); + + SCR_CaptureVideo_Ogg_Interleave(); + } + } if(cls.capturevideo.soundrate) { @@ -807,10 +758,13 @@ void SCR_CaptureVideo_Ogg_EndVideo() qvorbis_bitrate_addblock(&format->vb); while(qvorbis_bitrate_flushpacket(&format->vd, &pt)) qogg_stream_packetin(&format->vo, &pt); + SCR_CaptureVideo_Ogg_Interleave(); } } - if(qogg_stream_pageout(&format->to, &pg) > 0) + SCR_CaptureVideo_Ogg_FlushInterleaving(); + + 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); @@ -818,7 +772,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); @@ -828,7 +782,7 @@ 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); @@ -840,7 +794,7 @@ 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); @@ -854,27 +808,33 @@ void SCR_CaptureVideo_Ogg_EndVideo() qogg_stream_clear(&format->to); qtheora_clear(&format->ts); - - Mem_Free(format->yuv.y); - Mem_Free(format->yuv.u); - Mem_Free(format->yuv.v); + qvorbis_info_clear(&format->vi); + + Mem_Free(format->yuv[0].y); + Mem_Free(format->yuv[0].u); + Mem_Free(format->yuv[0].v); + Mem_Free(format->yuv[1].y); + Mem_Free(format->yuv[1].u); + Mem_Free(format->yuv[1].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(void) { - LOAD_FORMATSPECIFIC(); + 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; + yuv = &format->yuv[format->yuvi]; + for(y = 0; y < h; ++y) { for(b = cls.capturevideo.outbuffer + (h-1-y)*w*4, x = 0; x < w; ++x) @@ -882,21 +842,21 @@ void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV() blockr = b[2]; blockg = b[1]; blockb = b[0]; - format->yuv.y[x + format->yuv.y_stride * y] = + yuv->y[x + 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) + 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) { 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)] = + yuv->u[x + 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)] = + yuv->v[x + 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; } @@ -904,42 +864,64 @@ void SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV() } } -void SCR_CaptureVideo_Ogg_VideoFrame() +static void SCR_CaptureVideo_Ogg_VideoFrames(int num) { - LOAD_FORMATSPECIFIC(); - ogg_page pg; + LOAD_FORMATSPECIFIC_OGG(); 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) + if(format->yuvi >= 0) { - FS_Write(cls.capturevideo.videofile, pg.header, pg.header_len); - FS_Write(cls.capturevideo.videofile, pg.body, pg.body_len); + // send the previous frame + while(format->lastnum-- > 0) + { + qtheora_encode_YUVin(&format->ts, &format->yuv[format->yuvi]); + + while(qtheora_encode_packetout(&format->ts, false, &pt)) + qogg_stream_packetin(&format->to, &pt); + + SCR_CaptureVideo_Ogg_Interleave(); + } } + + format->yuvi = (format->yuvi + 1) % 2; + SCR_CaptureVideo_Ogg_ConvertFrame_BGRA_to_YUV(); + format->lastnum = num; + + // TODO maybe send num-1 frames from here already } -void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer, size_t length) +typedef int channelmapping_t[8]; +channelmapping_t mapping[8] = { - LOAD_FORMATSPECIFIC(); + { 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(); float **vorbis_buffer; size_t i; int j; - ogg_page pg; 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) { @@ -950,9 +932,190 @@ void SCR_CaptureVideo_Ogg_SoundFrame(const portable_sampleframe_t *paintbuffer, qogg_stream_packetin(&format->vo, &pt); } - while(qogg_stream_pageout(&format->vo, &pg) > 0) + SCR_CaptureVideo_Ogg_Interleave(); +} + +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(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; + cls.capturevideo.formatspecific = Mem_Alloc(tempmempool, sizeof(capturevideostate_ogg_formatspecific_t)); { + LOAD_FORMATSPECIFIC_OGG(); + int num, denom, i; + ogg_page pg; + ogg_packet pt, pt2, pt3; + theora_comment tc; + vorbis_comment vc; + theora_info ti; + int vp3compat; + + 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); + } + + format->videopage.len = format->audiopage.len = 0; + + 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; + + for(i = 0; i < 2; ++i) + { + format->yuv[i].y_width = ti.width; + format->yuv[i].y_height = ti.height; + format->yuv[i].y_stride = ti.width; + 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 = (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 / cls.capturevideo.framestep, &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) + { + ti.target_bitrate = -1; + ti.keyframe_data_target_bitrate = (unsigned int)-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 = 0x7FFFFFFF; + ti.keyframe_data_target_bitrate = 0x7FFFFFFF; + } + } + + // 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 + 1); + + 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) + { + 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 Sys_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 Sys_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); + } } }