theora: separate encode and push block in chain, into own function.
authorLuis de Bethencourt <luis.debethencourt@collabora.como>
Fri, 3 Jun 2011 14:29:00 +0000 (16:29 +0200)
committerSebastian Dröge <sebastian.droege@collabora.co.uk>
Sat, 4 Jun 2011 08:10:54 +0000 (10:10 +0200)
ext/theora/gsttheoraenc.c

index 6259be7..acc5bd6 100644 (file)
@@ -1144,6 +1144,83 @@ theora_enc_write_multipass_cache (GstTheoraEnc * enc, gboolean begin,
 }
 
 static GstFlowReturn
+theora_enc_encode_and_push (GstTheoraEnc * enc, ogg_packet op,
+    GstClockTime timestamp, GstClockTime running_time,
+    GstClockTime duration, GstBuffer * buffer)
+{
+  GstFlowReturn ret;
+  th_ycbcr_buffer ycbcr;
+  gint res;
+
+  theora_enc_init_buffer (ycbcr, &enc->info, GST_BUFFER_DATA (buffer));
+
+  if (theora_enc_is_discontinuous (enc, running_time, duration)) {
+    theora_enc_reset (enc);
+    enc->granulepos_offset =
+        gst_util_uint64_scale (running_time, enc->fps_n,
+        GST_SECOND * enc->fps_d);
+    enc->timestamp_offset = running_time;
+    enc->next_ts = 0;
+    enc->next_discont = TRUE;
+  }
+
+  if (enc->multipass_cache_fd
+      && enc->multipass_mode == MULTIPASS_MODE_SECOND_PASS) {
+    if (!theora_enc_read_multipass_cache (enc)) {
+      ret = GST_FLOW_ERROR;
+      goto multipass_read_failed;
+    }
+  }
+
+  res = th_encode_ycbcr_in (enc->encoder, ycbcr);
+  /* none of the failure cases can happen here */
+  g_assert (res == 0);
+
+  if (enc->multipass_cache_fd
+      && enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS) {
+    if (!theora_enc_write_multipass_cache (enc, FALSE, FALSE)) {
+      ret = GST_FLOW_ERROR;
+      goto multipass_write_failed;
+    }
+  }
+
+  ret = GST_FLOW_OK;
+  while (th_encode_packetout (enc->encoder, 0, &op)) {
+    GstClockTime next_time;
+
+    next_time = th_granule_time (enc->encoder, op.granulepos) * GST_SECOND;
+
+    ret =
+        theora_push_packet (enc, &op, timestamp, enc->next_ts,
+        next_time - enc->next_ts);
+
+    enc->next_ts = next_time;
+    if (ret != GST_FLOW_OK)
+      goto data_push;
+  }
+  gst_buffer_unref (buffer);
+
+  return ret;
+
+  /* ERRORS */
+multipass_read_failed:
+  {
+    gst_buffer_unref (buffer);
+    return ret;
+  }
+multipass_write_failed:
+  {
+    gst_buffer_unref (buffer);
+    return ret;
+  }
+data_push:
+  {
+    gst_buffer_unref (buffer);
+    return ret;
+  }
+}
+
+static GstFlowReturn
 theora_enc_chain (GstPad * pad, GstBuffer * buffer)
 {
   GstTheoraEnc *enc;
@@ -1291,72 +1368,12 @@ theora_enc_chain (GstPad * pad, GstBuffer * buffer)
     enc->next_ts = 0;
   }
 
-  {
-    th_ycbcr_buffer ycbcr;
-    gint res;
-
-    theora_enc_init_buffer (ycbcr, &enc->info, GST_BUFFER_DATA (buffer));
-
-    if (theora_enc_is_discontinuous (enc, running_time, duration)) {
-      theora_enc_reset (enc);
-      enc->granulepos_offset =
-          gst_util_uint64_scale (running_time, enc->fps_n,
-          GST_SECOND * enc->fps_d);
-      enc->timestamp_offset = running_time;
-      enc->next_ts = 0;
-      enc->next_discont = TRUE;
-    }
-
-    if (enc->multipass_cache_fd
-        && enc->multipass_mode == MULTIPASS_MODE_SECOND_PASS) {
-      if (!theora_enc_read_multipass_cache (enc)) {
-        ret = GST_FLOW_ERROR;
-        goto multipass_read_failed;
-      }
-    }
-
-    res = th_encode_ycbcr_in (enc->encoder, ycbcr);
-    /* none of the failure cases can happen here */
-    g_assert (res == 0);
-
-    if (enc->multipass_cache_fd
-        && enc->multipass_mode == MULTIPASS_MODE_FIRST_PASS) {
-      if (!theora_enc_write_multipass_cache (enc, FALSE, FALSE)) {
-        ret = GST_FLOW_ERROR;
-        goto multipass_write_failed;
-      }
-    }
-
-    ret = GST_FLOW_OK;
-    while (th_encode_packetout (enc->encoder, 0, &op)) {
-      GstClockTime next_time;
-
-      next_time = th_granule_time (enc->encoder, op.granulepos) * GST_SECOND;
-
-      ret =
-          theora_push_packet (enc, &op, timestamp, enc->next_ts,
-          next_time - enc->next_ts);
-
-      enc->next_ts = next_time;
-      if (ret != GST_FLOW_OK)
-        goto data_push;
-    }
-    gst_buffer_unref (buffer);
-  }
+  ret = theora_enc_encode_and_push (enc, op, timestamp, running_time, duration,
+      buffer);
 
   return ret;
 
   /* ERRORS */
-multipass_read_failed:
-  {
-    gst_buffer_unref (buffer);
-    return ret;
-  }
-multipass_write_failed:
-  {
-    gst_buffer_unref (buffer);
-    return ret;
-  }
 header_buffer_alloc:
   {
     gst_buffer_unref (buffer);
@@ -1367,11 +1384,6 @@ header_push:
     gst_buffer_unref (buffer);
     return ret;
   }
-data_push:
-  {
-    gst_buffer_unref (buffer);
-    return ret;
-  }
 encoder_disabled:
   {
     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),