--- gst/mpegdemux/gstmpegdemux.c	2009-08-11 00:50:05.000000000 +0200
+++ gst/mpegdemux/gstmpegdemux.c	2009-09-10 11:31:04.000000000 +0200
@@ -310,7 +309,10 @@
     if (stream != NULL) {
       if (stream->pad)
         gst_element_remove_pad (GST_ELEMENT_CAST (demux), stream->pad);
-
+      if (stream->buf)
+        free(stream->buf);
+      if (stream->meta_buf)
+        gst_buffer_unref(stream->meta_buf);
       g_free (stream);
       demux->streams[i] = NULL;
     }
@@ -427,6 +429,15 @@
   stream->type = stream_type;
   stream->pad = gst_pad_new_from_template (template, name);
   stream->segment_thresh = threshold;
+
+  if (threshold == VIDEO_SEGMENT_THRESHOLD) {
+    stream->buf = malloc(64*1024);
+    stream->buf_pos = 0;
+    stream->meta_buf = gst_buffer_new();
+  }
+  else
+    stream->buf = 0;
+
   gst_pad_set_event_function (stream->pad,
       GST_DEBUG_FUNCPTR (gst_flups_demux_src_event));
   gst_pad_set_query_function (stream->pad,
@@ -478,6 +488,7 @@
   GstFlowReturn result;
   guint64 timestamp;
   guint size;
+  gboolean sent = FALSE;
 
   if (stream == NULL)
     goto no_stream;
@@ -580,11 +591,38 @@
   demux->next_pts = G_MAXUINT64;
   demux->next_dts = G_MAXUINT64;
 
-  result = gst_pad_push (stream->pad, buf);
-  GST_DEBUG_OBJECT (demux, "pushed stream id 0x%02x type 0x%02x, time: %"
-      GST_TIME_FORMAT ", size %d. result: %s",
-      stream->id, stream->type, GST_TIME_ARGS (timestamp),
-      size, gst_flow_get_name (result));
+  if (stream->buf && stream->buf_pos && (timestamp != GST_CLOCK_TIME_NONE || stream->buf_pos+size > 64*1024)) {
+    GstBuffer *tmp = gst_buffer_new_and_alloc(stream->buf_pos);
+    gst_buffer_copy_metadata(tmp, stream->meta_buf, GST_BUFFER_COPY_ALL);
+    GST_BUFFER_SIZE(tmp) = stream->buf_pos;
+    memcpy(GST_BUFFER_DATA(tmp), stream->buf, stream->buf_pos);
+    result = gst_pad_push (stream->pad, tmp);
+    GST_DEBUG_OBJECT (demux, "pushed stream id 0x%02x type 0x%02x, time: %"
+        GST_TIME_FORMAT ", size %d. result: %s",
+        stream->id, stream->type, GST_TIME_ARGS (timestamp),
+        stream->buf_pos, gst_flow_get_name (result));
+    stream->buf_pos = 0;
+    sent = TRUE;
+  }
+
+  if (!stream->buf || size > 64*1024) {
+    result = gst_pad_push (stream->pad, buf);
+    GST_DEBUG_OBJECT (demux, "pushed stream id 0x%02x type 0x%02x, time: %"
+        GST_TIME_FORMAT ", size %d. result: %s",
+        stream->id, stream->type, GST_TIME_ARGS (timestamp),
+        size, gst_flow_get_name (result));
+  }
+  else {
+    if (!stream->buf_pos)
+      gst_buffer_copy_metadata(stream->meta_buf, buf, GST_BUFFER_COPY_ALL);
+
+    memcpy(stream->buf + stream->buf_pos, GST_BUFFER_DATA(buf), size);
+    stream->buf_pos += size;
+    gst_buffer_unref(buf);
+
+    if (!sent)
+      result = GST_FLOW_OK;
+  }
 
   return result;
 
--- gst/mpegdemux/gstmpegdemux.h	2009-08-11 00:50:05.000000000 +0200
+++ gst/mpegdemux/gstmpegdemux.h	2009-09-10 11:27:46.000000000 +0200
@@ -93,6 +93,10 @@
   gboolean discont;
   gboolean notlinked;
   gboolean need_segment;
+
+  GstBuffer *meta_buf;
+  unsigned char *buf;
+  size_t buf_pos;
 };
 
 struct _GstFluPSDemux
