+ 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");