+ ret = th_encode_ctl(pv->ctx, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
+ &keyframe_frequency, sizeof(keyframe_frequency));
+ if( ret < 0 )
+ {
+ hb_log("theora: Could not set keyframe interval to %d", keyframe_frequency);
+ }
+
+ /* Set "soft target" rate control which improves quality at the
+ * expense of solid bitrate caps */
+ int arg = TH_RATECTL_CAP_UNDERFLOW;
+ ret = th_encode_ctl(pv->ctx, TH_ENCCTL_SET_RATE_FLAGS, &arg, sizeof(arg));
+ if( ret < 0 )
+ {
+ hb_log("theora: Could not set soft ratecontrol");
+ }
+ if( job->pass != 0 && job->pass != -1 )
+ {
+ arg = keyframe_frequency * 7 >> 1;
+ ret = th_encode_ctl(pv->ctx, TH_ENCCTL_SET_RATE_BUFFER, &arg, sizeof(arg));
+ if( ret < 0 )
+ {
+ hb_log("theora: Could not set rate control buffer");
+ }
+ }
+
+ if( job->pass == 1 )
+ {
+ unsigned char *buffer;
+ int bytes;
+ bytes = th_encode_ctl(pv->ctx, TH_ENCCTL_2PASS_OUT, &buffer, sizeof(buffer));
+ if( bytes < 0 )
+ {
+ hb_error("Could not set up the first pass of two-pass mode.\n");
+ hb_error("Did you remember to specify an estimated bitrate?\n");
+ return 1;
+ }
+ if( fwrite( buffer, 1, bytes, pv->file ) < bytes )
+ {
+ hb_error("Unable to write to two-pass data file.\n");
+ return 1;
+ }
+ fflush( pv->file );
+ }
+ if( job->pass == 2 )
+ {
+ /* Enable the second pass here.
+ * We make this call just to set the encoder into 2-pass mode, because
+ * by default enabling two-pass sets the buffer delay to the whole file
+ * (because there's no way to explicitly request that behavior).
+ * If we waited until we were actually encoding, it would overwite our
+ * settings.*/
+ hb_log("enctheora: init 2nd pass");
+ if( th_encode_ctl( pv->ctx, TH_ENCCTL_2PASS_IN, NULL, 0) < 0)
+ {
+ hb_log("theora: Could not set up the second pass of two-pass mode.");
+ return 1;
+ }
+ }
+
+ th_comment_init( &tc );
+
+ th_encode_flushheader( pv->ctx, &tc, &op );