theoraenc: Clean up handling of reset/flushing/start/stop

This commit is contained in:
Sebastian Dröge 2013-07-25 13:31:07 +02:00
parent 775aa4100b
commit 239b623a9c

View file

@ -177,6 +177,7 @@ G_DEFINE_TYPE (GstTheoraEnc, gst_theora_enc, GST_TYPE_VIDEO_ENCODER);
static gboolean theora_enc_start (GstVideoEncoder * enc); static gboolean theora_enc_start (GstVideoEncoder * enc);
static gboolean theora_enc_stop (GstVideoEncoder * enc); static gboolean theora_enc_stop (GstVideoEncoder * enc);
static gboolean theora_enc_reset (GstVideoEncoder * enc, gboolean hard);
static gboolean theora_enc_set_format (GstVideoEncoder * enc, static gboolean theora_enc_set_format (GstVideoEncoder * enc,
GstVideoCodecState * state); GstVideoCodecState * state);
static GstFlowReturn theora_enc_handle_frame (GstVideoEncoder * enc, static GstFlowReturn theora_enc_handle_frame (GstVideoEncoder * enc,
@ -221,6 +222,7 @@ gst_theora_enc_class_init (GstTheoraEncClass * klass)
gstvideo_encoder_class->start = GST_DEBUG_FUNCPTR (theora_enc_start); gstvideo_encoder_class->start = GST_DEBUG_FUNCPTR (theora_enc_start);
gstvideo_encoder_class->stop = GST_DEBUG_FUNCPTR (theora_enc_stop); gstvideo_encoder_class->stop = GST_DEBUG_FUNCPTR (theora_enc_stop);
gstvideo_encoder_class->reset = GST_DEBUG_FUNCPTR (theora_enc_reset);
gstvideo_encoder_class->set_format = gstvideo_encoder_class->set_format =
GST_DEBUG_FUNCPTR (theora_enc_set_format); GST_DEBUG_FUNCPTR (theora_enc_set_format);
gstvideo_encoder_class->handle_frame = gstvideo_encoder_class->handle_frame =
@ -355,9 +357,10 @@ theora_enc_finalize (GObject * object)
G_OBJECT_CLASS (parent_class)->finalize (object); G_OBJECT_CLASS (parent_class)->finalize (object);
} }
static void static gboolean
theora_enc_reset (GstTheoraEnc * enc) theora_enc_reset (GstVideoEncoder * encoder, gboolean hard)
{ {
GstTheoraEnc *enc = GST_THEORA_ENC (encoder);
ogg_uint32_t keyframe_force; ogg_uint32_t keyframe_force;
int rate_flags; int rate_flags;
@ -370,6 +373,8 @@ theora_enc_reset (GstTheoraEnc * enc)
if (enc->encoder) if (enc->encoder)
th_encode_free (enc->encoder); th_encode_free (enc->encoder);
if (!hard) {
enc->encoder = th_encode_alloc (&enc->info); enc->encoder = th_encode_alloc (&enc->info);
/* We ensure this function cannot fail. */ /* We ensure this function cannot fail. */
g_assert (enc->encoder != NULL); g_assert (enc->encoder != NULL);
@ -404,6 +409,20 @@ theora_enc_reset (GstTheoraEnc * enc)
if (enc->multipass_cache_fd if (enc->multipass_cache_fd
&& enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS) && enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS)
theora_enc_write_multipass_cache (enc, TRUE, FALSE); theora_enc_write_multipass_cache (enc, TRUE, FALSE);
} else {
enc->encoder = NULL;
th_comment_clear (&enc->comment);
th_info_clear (&enc->info);
if (enc->input_state)
gst_video_codec_state_unref (enc->input_state);
enc->input_state = NULL;
enc->packetno = 0;
enc->initialised = FALSE;
}
return TRUE;
} }
static gboolean static gboolean
@ -414,10 +433,6 @@ theora_enc_start (GstVideoEncoder * benc)
GST_DEBUG_OBJECT (benc, "start: init theora"); GST_DEBUG_OBJECT (benc, "start: init theora");
enc = GST_THEORA_ENC (benc); enc = GST_THEORA_ENC (benc);
th_info_init (&enc->info);
th_comment_init (&enc->comment);
enc->packetno = 0;
if (enc->multipass_mode >= MULTIPASS_MODE_FIRST_PASS) { if (enc->multipass_mode >= MULTIPASS_MODE_FIRST_PASS) {
GError *err = NULL; GError *err = NULL;
@ -453,14 +468,8 @@ theora_enc_stop (GstVideoEncoder * benc)
GST_DEBUG_OBJECT (benc, "stop: clearing theora state"); GST_DEBUG_OBJECT (benc, "stop: clearing theora state");
enc = GST_THEORA_ENC (benc); enc = GST_THEORA_ENC (benc);
if (enc->encoder) { /* Everything else is handled in reset() */
th_encode_free (enc->encoder); theora_enc_clear_multipass_cache (enc);
enc->encoder = NULL;
}
th_comment_clear (&enc->comment);
th_info_clear (&enc->info);
enc->initialised = FALSE;
return TRUE; return TRUE;
} }
@ -587,7 +596,7 @@ theora_enc_set_format (GstVideoEncoder * benc, GstVideoCodecState * state)
"keyframe_frequency_force is %d, granule shift is %d", "keyframe_frequency_force is %d, granule shift is %d",
enc->keyframe_force, enc->info.keyframe_granule_shift); enc->keyframe_force, enc->info.keyframe_granule_shift);
theora_enc_reset (enc); theora_enc_reset (benc, FALSE);
enc->initialised = TRUE; enc->initialised = TRUE;
return TRUE; return TRUE;