Merge branch 'master' into 0.11
[platform/upstream/gstreamer.git] / libs / gst / base / gstbaseparse.c
index cb3b3c6..6aa1db8 100644 (file)
  *       contain a valid frame, this call must return FALSE and optionally
  *       set the @skipsize value to inform base class that how many bytes
  *       it needs to skip in order to find a valid frame. @framesize can always
- *       indicate a new minimum for current frame parsing.  The passed buffer
+ *       indicate a new minimum for current frame parsing.  Indicating G_MAXUINT
+ *       for requested amount means subclass simply needs best available
+ *       subsequent data.  In push mode this amounts to an additional input buffer
+ *       (thus minimal additional latency), in pull mode this amounts to some
+ *       arbitrary reasonable buffer size increase.  The passed buffer
  *       is read-only.  Note that @check_valid_frame might receive any small
  *       amount of input data when leftover data is being drained (e.g. at EOS).
  *     </para></listitem>
 
 #include "gstbaseparse.h"
 
+#define GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC  (1 << 0)
+
 #define MIN_FRAMES_TO_POST_BITRATE 10
 #define TARGET_DIFFERENCE          (20 * GST_SECOND)
 
@@ -206,7 +212,7 @@ GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
 #define GST_CAT_DEFAULT gst_base_parse_debug
 
 /* Supported formats */
-static GstFormat fmtlist[] = {
+static const GstFormat fmtlist[] = {
   GST_FORMAT_DEFAULT,
   GST_FORMAT_BYTES,
   GST_FORMAT_TIME,
@@ -235,6 +241,7 @@ struct _GstBaseParsePrivate
   guint bitrate;
   guint lead_in, lead_out;
   GstClockTime lead_in_ts, lead_out_ts;
+  GstClockTime min_latency, max_latency;
 
   gboolean discont;
   gboolean flushing;
@@ -274,6 +281,8 @@ struct _GstBaseParsePrivate
   GstIndex *index;
   gint index_id;
   gboolean own_index;
+  GStaticMutex index_lock;
+
   /* seek table entries only maintained if upstream is BYTE seekable */
   gboolean upstream_seekable;
   gboolean upstream_has_duration;
@@ -302,6 +311,9 @@ struct _GstBaseParsePrivate
 
   /* Segment event that closes the running segment prior to SEEK */
   GstEvent *close_segment;
+
+  /* push mode helper frame */
+  GstBaseParseFrame frame;
 };
 
 typedef struct _GstBaseParseSeek
@@ -365,7 +377,6 @@ static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
-static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
 
 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
@@ -397,10 +408,6 @@ static GstFlowReturn gst_base_parse_process_fragment (GstBaseParse * parse,
 
 static gboolean gst_base_parse_is_seekable (GstBaseParse * parse);
 
-static void gst_base_parse_frame_free (GstBaseParseFrame * frame);
-static GstBaseParseFrame *gst_base_parse_frame_copy_and_clear (GstBaseParseFrame
-    * frame);
-
 static void
 gst_base_parse_clear_queues (GstBaseParse * parse)
 {
@@ -452,6 +459,8 @@ gst_base_parse_finalize (GObject * object)
     parse->priv->index = NULL;
   }
 
+  g_static_mutex_free (&parse->priv->index_lock);
+
   gst_base_parse_clear_queues (parse);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -499,8 +508,6 @@ gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
   parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
   gst_pad_set_event_function (parse->sinkpad,
       GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
-  gst_pad_set_setcaps_function (parse->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
   gst_pad_set_chain_function (parse->sinkpad,
       GST_DEBUG_FUNCPTR (gst_base_parse_chain));
   gst_pad_set_activate_function (parse->sinkpad,
@@ -533,55 +540,109 @@ gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
 
   parse->priv->pad_mode = GST_ACTIVATE_NONE;
 
+  g_static_mutex_init (&parse->priv->index_lock);
+
   /* init state */
   gst_base_parse_reset (parse);
   GST_DEBUG_OBJECT (parse, "init ok");
 }
 
-/**
- * gst_base_parse_frame_init:
- * @parse: #GstBaseParse.
- * @frame: #GstBaseParseFrame.
- *
- * Sets a #GstBaseParseFrame to initial state.  Currently this means
- * all fields are zero-ed.
- *
- * Since: 0.10.33
- */
-void
-gst_base_parse_frame_init (GstBaseParse * parse, GstBaseParseFrame * frame)
+static GstBaseParseFrame *
+gst_base_parse_frame_copy (GstBaseParseFrame * frame)
 {
-  memset (frame, 0, sizeof (*frame));
+  GstBaseParseFrame *copy;
+
+  copy = g_slice_dup (GstBaseParseFrame, frame);
+  copy->buffer = gst_buffer_ref (frame->buffer);
+  copy->_private_flags &= ~GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
+
+  GST_TRACE ("copied frame %p -> %p", frame, copy);
+
+  return copy;
 }
 
-/* clear == frame no longer to be used following this */
-static void
-gst_base_parse_frame_clear (GstBaseParse * parse, GstBaseParseFrame * frame)
+void
+gst_base_parse_frame_free (GstBaseParseFrame * frame)
 {
-  /* limited for now */
+  GST_TRACE ("freeing frame %p", frame);
+
   if (frame->buffer) {
     gst_buffer_unref (frame->buffer);
     frame->buffer = NULL;
   }
+
+  if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) {
+    g_slice_free (GstBaseParseFrame, frame);
+  } else {
+    memset (frame, 0, sizeof (*frame));
+  }
 }
 
-/* free frame allocated with copy_and_clear (and not on the stack) */
-static void
-gst_base_parse_frame_free (GstBaseParseFrame * frame)
+GType
+gst_base_parse_frame_get_type (void)
 {
-  gst_base_parse_frame_clear (NULL, frame);
-  g_slice_free (GstBaseParseFrame, frame);
+  static volatile gsize frame_type = 0;
+
+  if (g_once_init_enter (&frame_type)) {
+    GType _type;
+
+    _type = g_boxed_type_register_static ("GstBaseParseFrame",
+        (GBoxedCopyFunc) gst_base_parse_frame_copy,
+        (GBoxedFreeFunc) gst_base_parse_frame_free);
+    g_once_init_leave (&frame_type, _type);
+  }
+  return (GType) frame_type;
 }
 
-/* copy frame (taking ownership of contents of passed frame) */
-static GstBaseParseFrame *
-gst_base_parse_frame_copy_and_clear (GstBaseParseFrame * frame)
+/**
+ * gst_base_parse_frame_init:
+ * @frame: #GstBaseParseFrame.
+ *
+ * Sets a #GstBaseParseFrame to initial state.  Currently this means
+ * all public fields are zero-ed and a private flag is set to make
+ * sure gst_base_parse_frame_free() only frees the contents but not
+ * the actual frame. Use this function to initialise a #GstBaseParseFrame
+ * allocated on the stack.
+ *
+ * Since: 0.10.33
+ */
+void
+gst_base_parse_frame_init (GstBaseParseFrame * frame)
 {
-  GstBaseParseFrame *copy;
-
-  copy = g_slice_dup (GstBaseParseFrame, frame);
   memset (frame, 0, sizeof (GstBaseParseFrame));
-  return copy;
+  frame->_private_flags = GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
+  GST_TRACE ("inited frame %p", frame);
+}
+
+/**
+ * gst_base_parse_frame_new:
+ * @buffer: (transfer none): a #GstBuffer
+ * @flags: the flags
+ * @overhead: number of bytes in this frame which should be counted as
+ *     metadata overhead, ie. not used to calculate the average bitrate.
+ *     Set to -1 to mark the entire frame as metadata. If in doubt, set to 0.
+ *
+ * Allocates a new #GstBaseParseFrame. This function is mainly for bindings,
+ * elements written in C should usually allocate the frame on the stack and
+ * then use gst_base_parse_frame_init() to initialise it.
+ *
+ * Returns: a newly-allocated #GstBaseParseFrame. Free with
+ *     gst_base_parse_frame_free() when no longer needed, unless you gave
+ *     away ownership to gst_base_parse_push_frame().
+ *
+ * Since: 0.10.33
+ */
+GstBaseParseFrame *
+gst_base_parse_frame_new (GstBuffer * buffer, GstBaseParseFrameFlags flags,
+    gint overhead)
+{
+  GstBaseParseFrame *frame;
+
+  frame = g_slice_new0 (GstBaseParseFrame);
+  frame->buffer = gst_buffer_ref (buffer);
+
+  GST_TRACE ("created frame %p", frame);
+  return frame;
 }
 
 static inline void
@@ -667,6 +728,11 @@ gst_base_parse_reset (GstBaseParse * parse)
   g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
   g_slist_free (parse->priv->pending_seeks);
   parse->priv->pending_seeks = NULL;
+
+  /* we know it is not alloc'ed, but maybe other stuff to free, some day ... */
+  parse->priv->frame._private_flags |=
+      GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
+  gst_base_parse_frame_free (&parse->priv->frame);
   GST_OBJECT_UNLOCK (parse);
 }
 
@@ -685,7 +751,7 @@ static gboolean
 gst_base_parse_check_frame (GstBaseParse * parse,
     GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
 {
-  *framesize = GST_BUFFER_SIZE (frame->buffer);
+  *framesize = gst_buffer_get_size (frame->buffer);
   *skipsize = 0;
   return TRUE;
 }
@@ -789,10 +855,10 @@ gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
   GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
       GST_EVENT_TYPE_NAME (event));
 
-  /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
+  /* Cache all events except EOS, SEGMENT and FLUSH_STOP if we have a
    * pending segment */
   if (parse->priv->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
-      && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
+      && GST_EVENT_TYPE (event) != GST_EVENT_SEGMENT
       && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
       && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
 
@@ -844,36 +910,52 @@ gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
   GstEvent **eventp;
 
   switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_NEWSEGMENT:
+    case GST_EVENT_CAPS:
     {
+      GstCaps *caps;
+      GstBaseParseClass *klass;
+
+      klass = GST_BASE_PARSE_GET_CLASS (parse);
+
+      gst_event_parse_caps (event, &caps);
+      GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
+
+      if (klass->set_sink_caps)
+        klass->set_sink_caps (parse, caps);
+
+      handled = TRUE;
+      break;
+    }
+    case GST_EVENT_SEGMENT:
+    {
+      const GstSegment *in_segment;
+      GstSegment out_segment;
+      gint64 offset = 0, next_ts;
+
+#if 0
       gdouble rate, applied_rate;
       GstFormat format;
-      gint64 start, stop, pos, next_ts, offset = 0;
+      gint64 start, stop, pos, next_ts;
       gboolean update;
+#endif
 
-      gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
-          &format, &start, &stop, &pos);
+      gst_event_parse_segment (event, &in_segment);
+      gst_segment_init (&out_segment, GST_FORMAT_TIME);
 
-      GST_DEBUG_OBJECT (parse, "newseg rate %g, applied rate %g, "
-          "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
-          ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
-          GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
+      GST_DEBUG_OBJECT (parse, "segment %" GST_SEGMENT_FORMAT, in_segment);
 
-      if (format == GST_FORMAT_BYTES) {
-        GstClockTime seg_start, seg_stop;
+      if (in_segment->format == GST_FORMAT_BYTES) {
         GstBaseParseSeek *seek = NULL;
         GSList *node;
 
         /* stop time is allowed to be open-ended, but not start & pos */
-        seg_stop = GST_CLOCK_TIME_NONE;
-        seg_start = 0;
-        offset = pos;
+        offset = in_segment->time;
 
         GST_OBJECT_LOCK (parse);
         for (node = parse->priv->pending_seeks; node; node = node->next) {
           GstBaseParseSeek *tmp = node->data;
 
-          if (tmp->offset == pos) {
+          if (tmp->offset == offset) {
             seek = tmp;
             break;
           }
@@ -886,8 +968,11 @@ gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
           GST_DEBUG_OBJECT (parse,
               "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
               seek->accurate ? " accurate" : "", &seek->segment);
-          seg_start = seek->segment.start;
-          seg_stop = seek->segment.stop;
+
+          out_segment.start = seek->segment.start;
+          out_segment.stop = seek->segment.stop;
+          out_segment.time = seek->segment.start;
+
           next_ts = seek->start_ts;
           parse->priv->exact_position = seek->accurate;
           g_free (seek);
@@ -895,39 +980,48 @@ gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
           /* best attempt convert */
           /* as these are only estimates, stop is kept open-ended to avoid
            * premature cutting */
-          gst_base_parse_convert (parse, GST_FORMAT_BYTES, start,
-              GST_FORMAT_TIME, (gint64 *) & seg_start);
-          parse->priv->exact_position = (start == 0);
-          next_ts = seg_start;
+          gst_base_parse_convert (parse, GST_FORMAT_BYTES, in_segment->start,
+              GST_FORMAT_TIME, (gint64 *) & next_ts);
+
+          out_segment.start = next_ts;
+          out_segment.stop = GST_CLOCK_TIME_NONE;
+          out_segment.time = next_ts;
+
+          parse->priv->exact_position = (in_segment->start == 0);
         }
 
         gst_event_unref (event);
-        event = gst_event_new_new_segment_full (update, rate, applied_rate,
-            GST_FORMAT_TIME, seg_start, seg_stop, seg_start);
-        format = GST_FORMAT_TIME;
-        start = seg_start;
-        stop = seg_stop;
+
+        event = gst_event_new_segment (&out_segment);
+
         GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
-            "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT,
-            GST_TIME_ARGS (seg_start), GST_TIME_ARGS (seg_stop));
-      } else if (format != GST_FORMAT_TIME) {
+            GST_SEGMENT_FORMAT, in_segment);
+
+      } else if (in_segment->format != GST_FORMAT_TIME) {
         /* Unknown incoming segment format. Output a default open-ended
          * TIME segment */
         gst_event_unref (event);
-        event = gst_event_new_new_segment_full (update, rate, applied_rate,
-            GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
-        format = GST_FORMAT_TIME;
-        next_ts = start = 0;
-        stop = GST_CLOCK_TIME_NONE;
+
+        out_segment.start = 0;
+        out_segment.stop = GST_CLOCK_TIME_NONE;;
+        out_segment.time = 0;;
+
+        event = gst_event_new_segment (&out_segment);
+
+        next_ts = 0;
       } else {
         /* not considered BYTE seekable if it is talking to us in TIME,
          * whatever else it might claim */
         parse->priv->upstream_seekable = FALSE;
-        next_ts = start;
+        next_ts = in_segment->start;
       }
 
-      gst_segment_set_newsegment_full (&parse->segment, update, rate,
-          applied_rate, format, start, stop, start);
+      memcpy (&parse->segment, &out_segment, sizeof (GstSegment));
+
+      /*
+         gst_segment_set_newsegment (&parse->segment, update, rate,
+         applied_rate, format, start, stop, start);
+       */
 
       /* save the segment for later, right before we push a new buffer so that
        * the caps are fixed and the next linked element can receive
@@ -939,11 +1033,12 @@ gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
 
       /* but finish the current segment */
       GST_DEBUG_OBJECT (parse, "draining current segment");
-      if (parse->segment.rate > 0.0)
+      if (in_segment->rate > 0.0)
         gst_base_parse_drain (parse);
       else
         gst_base_parse_process_fragment (parse, FALSE);
       gst_adapter_clear (parse->priv->adapter);
+
       parse->priv->offset = offset;
       parse->priv->sync_offset = offset;
       parse->priv->next_ts = next_ts;
@@ -970,6 +1065,9 @@ gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
       parse->priv->flushing = FALSE;
       parse->priv->discont = TRUE;
       parse->priv->last_ts = GST_CLOCK_TIME_NONE;
+      parse->priv->frame._private_flags |=
+          GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
+      gst_base_parse_frame_free (&parse->priv->frame);
       break;
 
     case GST_EVENT_EOS:
@@ -1180,27 +1278,40 @@ static void
 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
     gboolean post_avg, gboolean post_max)
 {
-  GstTagList *taglist = gst_tag_list_new ();
+  GstTagList *taglist = NULL;
+
+  if (post_min && parse->priv->post_min_bitrate) {
+    taglist = gst_tag_list_new ();
 
-  if (post_min && parse->priv->post_min_bitrate)
     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
         GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
+  }
 
   if (post_avg && parse->priv->post_avg_bitrate) {
+    if (taglist == NULL)
+      taglist = gst_tag_list_new ();
+
     parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
         parse->priv->avg_bitrate, NULL);
   }
 
-  if (post_max && parse->priv->post_max_bitrate)
+  if (post_max && parse->priv->post_max_bitrate) {
+    if (taglist == NULL)
+      taglist = gst_tag_list_new ();
+
     gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
         GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
+  }
 
   GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
       parse->priv->min_bitrate, parse->priv->avg_bitrate,
       parse->priv->max_bitrate);
 
-  gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
+  if (taglist != NULL) {
+    gst_element_found_tags_for_pad (GST_ELEMENT_CAST (parse), parse->srcpad,
+        taglist);
+  }
 }
 
 /* gst_base_parse_update_bitrates:
@@ -1216,19 +1327,16 @@ gst_base_parse_update_bitrates (GstBaseParse * parse, GstBaseParseFrame * frame)
   /* Only update the tag on a 10 kbps delta */
   static const gint update_threshold = 10000;
 
-  GstBaseParseClass *klass;
   guint64 data_len, frame_dur;
   gint overhead, frame_bitrate, old_avg_bitrate;
   gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
   GstBuffer *buffer = frame->buffer;
 
-  klass = GST_BASE_PARSE_GET_CLASS (parse);
-
   overhead = frame->overhead;
   if (overhead == -1)
     return;
 
-  data_len = GST_BUFFER_SIZE (buffer) - overhead;
+  data_len = gst_buffer_get_size (buffer) - overhead;
   parse->priv->data_bytecount += data_len;
 
   /* duration should be valid by now,
@@ -1372,11 +1480,11 @@ gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
   associations[1].value = offset;
 
   /* index might change on-the-fly, although that would be nutty app ... */
-  GST_OBJECT_LOCK (parse);
+  g_static_mutex_lock (&parse->priv->index_lock);
   gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
       (key) ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_DELTA_UNIT,
       2, (const GstIndexAssociation *) &associations);
-  GST_OBJECT_UNLOCK (parse);
+  g_static_mutex_unlock (&parse->priv->index_lock);
 
   if (key) {
     parse->priv->index_last_offset = offset;
@@ -1469,7 +1577,7 @@ gst_base_parse_check_media (GstBaseParse * parse)
   GstCaps *caps;
   GstStructure *s;
 
-  caps = GST_PAD_CAPS (parse->srcpad);
+  caps = gst_pad_get_current_caps (parse->srcpad);
   if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) {
     parse->priv->is_video =
         g_str_has_prefix (gst_structure_get_name (s), "video");
@@ -1477,14 +1585,35 @@ gst_base_parse_check_media (GstBaseParse * parse)
     /* historical default */
     parse->priv->is_video = FALSE;
   }
+  if (caps)
+    gst_caps_unref (caps);
 
   GST_DEBUG_OBJECT (parse, "media is video == %d", parse->priv->is_video);
 }
 
+/* takes ownership of frame */
+static void
+gst_base_parse_queue_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
+{
+  if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) {
+    /* frame allocated on the heap, we can just take ownership */
+    g_queue_push_tail (&parse->priv->queued_frames, frame);
+    GST_TRACE ("queued frame %p", frame);
+  } else {
+    GstBaseParseFrame *copy;
+
+    /* probably allocated on the stack, must make a proper copy */
+    copy = gst_base_parse_frame_copy (frame);
+    g_queue_push_tail (&parse->priv->queued_frames, copy);
+    GST_TRACE ("queued frame %p (copy of %p)", copy, frame);
+    gst_base_parse_frame_free (frame);
+  }
+}
+
 /* gst_base_parse_handle_and_push_buffer:
  * @parse: #GstBaseParse.
  * @klass: #GstBaseParseClass.
- * @buffer: #GstBuffer.
+ * @frame: (transfer full): a #GstBaseParseFrame
  *
  * Parses the frame from given buffer and pushes it forward. Also performs
  * timestamp handling and checks the segment limits.
@@ -1521,7 +1650,7 @@ gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
       "parsing frame at offset %" G_GUINT64_FORMAT
       " (%#" G_GINT64_MODIFIER "x) of size %d",
       GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
-      GST_BUFFER_SIZE (buffer));
+      gst_buffer_get_size (buffer));
 
   /* use default handler to provide initial (upstream) metadata */
   gst_base_parse_parse_frame (parse, frame);
@@ -1584,11 +1713,10 @@ gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
    * frames to decide on the format and queues them internally */
   /* convert internal flow to OK and mark discont for the next buffer. */
   if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
-    gst_base_parse_frame_clear (parse, frame);
+    gst_base_parse_frame_free (frame);
     return GST_FLOW_OK;
   } else if (ret == GST_BASE_PARSE_FLOW_QUEUED) {
-    g_queue_push_tail (&parse->priv->queued_frames,
-        gst_base_parse_frame_copy_and_clear (frame));
+    gst_base_parse_queue_frame (parse, frame);
     return GST_FLOW_OK;
   } else if (ret != GST_FLOW_OK) {
     return ret;
@@ -1599,10 +1727,6 @@ gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
     GstBaseParseFrame *queued_frame;
 
     while ((queued_frame = g_queue_pop_head (&parse->priv->queued_frames))) {
-      queued_frame->buffer =
-          gst_buffer_make_metadata_writable (queued_frame->buffer);
-      gst_buffer_set_caps (queued_frame->buffer,
-          GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (parse)));
       gst_base_parse_push_frame (parse, queued_frame);
       gst_base_parse_frame_free (queued_frame);
     }
@@ -1614,10 +1738,12 @@ gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
 /**
  * gst_base_parse_push_frame:
  * @parse: #GstBaseParse.
- * @frame: #GstBaseParseFrame.
+ * @frame: (transfer full): a #GstBaseParseFrame
  *
  * Pushes the frame downstream, sends any pending events and
- * does some timestamp and segment handling.
+ * does some timestamp and segment handling. Takes ownership
+ * of @frame and will clear it (if it was initialised with
+ * gst_base_parse_frame_init()) or free it.
  *
  * This must be called with sinkpad STREAM_LOCK held.
  *
@@ -1633,20 +1759,24 @@ gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
   GstClockTime last_stop = GST_CLOCK_TIME_NONE;
   GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
   GstBuffer *buffer;
+  gsize size;
 
   g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
   g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
 
+  GST_TRACE_OBJECT (parse, "pushing frame %p", frame);
+
   buffer = frame->buffer;
 
   GST_LOG_OBJECT (parse,
       "processing buffer of size %d with ts %" GST_TIME_FORMAT
-      ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
+      ", duration %" GST_TIME_FORMAT, gst_buffer_get_size (buffer),
       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
 
   /* update stats */
-  parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
+  size = gst_buffer_get_size (buffer);
+  parse->priv->bytecount += size;
   if (G_LIKELY (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_NO_FRAME))) {
     parse->priv->framecount++;
     if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
@@ -1667,7 +1797,8 @@ gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
     last_stop = last_start + GST_BUFFER_DURATION (buffer);
 
   /* should have caps by now */
-  g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
+  g_return_val_if_fail (gst_pad_has_current_caps (parse->srcpad),
+      GST_FLOW_ERROR);
 
   /* segment adjustment magic; only if we are running the whole show */
   if (!parse->priv->passthrough && parse->segment.rate > 0.0 &&
@@ -1681,60 +1812,46 @@ gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
       gst_event_unref (parse->priv->pending_segment);
       parse->segment.start =
           MIN ((guint64) last_start, (guint64) parse->segment.stop);
+
       GST_DEBUG_OBJECT (parse,
           "adjusting pending segment start to %" GST_TIME_FORMAT,
           GST_TIME_ARGS (parse->segment.start));
-      parse->priv->pending_segment =
-          gst_event_new_new_segment (FALSE, parse->segment.rate,
-          parse->segment.format, parse->segment.start,
-          parse->segment.stop, parse->segment.start);
+
+      parse->priv->pending_segment = gst_event_new_segment (&parse->segment);
     }
     /* handle gaps, e.g. non-zero start-time, in as much not handled by above */
-    if (GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop) &&
+    if (GST_CLOCK_TIME_IS_VALID (parse->segment.position) &&
         GST_CLOCK_TIME_IS_VALID (last_start)) {
       GstClockTimeDiff diff;
 
       /* only send newsegments with increasing start times,
        * otherwise if these go back and forth downstream (sinks) increase
        * accumulated time and running_time */
-      diff = GST_CLOCK_DIFF (parse->segment.last_stop, last_start);
+      diff = GST_CLOCK_DIFF (parse->segment.position, last_start);
       if (G_UNLIKELY (diff > 2 * GST_SECOND
               && last_start > parse->segment.start
               && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop)
                   || last_start < parse->segment.stop))) {
+
         GST_DEBUG_OBJECT (parse,
             "Gap of %" G_GINT64_FORMAT " ns detected in stream " "(%"
             GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
             "Sending updated NEWSEGMENT events", diff,
-            GST_TIME_ARGS (parse->segment.last_stop),
+            GST_TIME_ARGS (parse->segment.position),
             GST_TIME_ARGS (last_start));
+
         if (G_UNLIKELY (parse->priv->pending_segment)) {
           gst_event_unref (parse->priv->pending_segment);
           parse->segment.start = last_start;
+          parse->segment.time = last_start;
           parse->priv->pending_segment =
-              gst_event_new_new_segment (FALSE, parse->segment.rate,
-              parse->segment.format, parse->segment.start,
-              parse->segment.stop, parse->segment.start);
+              gst_event_new_segment (&parse->segment);
         } else {
-          /* send newsegment events such that the gap is not accounted in
-           * accum time, hence running_time */
-          /* close ahead of gap */
-          gst_pad_push_event (parse->srcpad,
-              gst_event_new_new_segment (TRUE, parse->segment.rate,
-                  parse->segment.format, parse->segment.last_stop,
-                  parse->segment.last_stop, parse->segment.last_stop));
-          /* skip gap */
+          /* skip gap FIXME */
           gst_pad_push_event (parse->srcpad,
-              gst_event_new_new_segment (FALSE, parse->segment.rate,
-                  parse->segment.format, last_start,
-                  parse->segment.stop, last_start));
+              gst_event_new_segment (&parse->segment));
         }
-        /* align segment view with downstream,
-         * prevents double-counting accum when closing segment */
-        gst_segment_set_newsegment (&parse->segment, FALSE,
-            parse->segment.rate, parse->segment.format, last_start,
-            parse->segment.stop, last_start);
-        parse->segment.last_stop = last_start;
+        parse->segment.position = last_start;
       }
     }
   }
@@ -1783,10 +1900,6 @@ gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
   /* subclass must play nice */
   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
 
-  /* decorate */
-  buffer = gst_buffer_make_metadata_writable (buffer);
-  gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
-
   parse->priv->seen_keyframe |= parse->priv->is_video &&
       !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
 
@@ -1815,26 +1928,25 @@ gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
   }
 
   if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
-    GST_LOG_OBJECT (parse, "frame (%d bytes) dropped",
-        GST_BUFFER_SIZE (buffer));
+    GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) dropped", size);
     gst_buffer_unref (buffer);
     ret = GST_FLOW_OK;
   } else if (ret == GST_FLOW_OK) {
     if (parse->segment.rate > 0.0) {
       ret = gst_pad_push (parse->srcpad, buffer);
-      GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %s",
-          GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
+      GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) pushed: %s",
+          size, gst_flow_get_name (ret));
     } else {
-      GST_LOG_OBJECT (parse, "frame (%d bytes) queued for now",
-          GST_BUFFER_SIZE (buffer));
+      GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) queued for now",
+          size);
       parse->priv->buffers_queued =
           g_slist_prepend (parse->priv->buffers_queued, buffer);
       ret = GST_FLOW_OK;
     }
   } else {
     gst_buffer_unref (buffer);
-    GST_LOG_OBJECT (parse, "frame (%d bytes) not pushed: %s",
-        GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
+    GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s",
+        size, gst_flow_get_name (ret));
     /* if we are not sufficiently in control, let upstream decide on EOS */
     if (ret == GST_FLOW_UNEXPECTED &&
         (parse->priv->passthrough ||
@@ -1845,10 +1957,10 @@ gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
 
   /* Update current running segment position */
   if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE &&
-      parse->segment.last_stop < last_stop)
-    gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
+      parse->segment.position < last_stop)
+    parse->segment.position = last_stop;
 
-  gst_base_parse_frame_clear (parse, frame);
+  gst_base_parse_frame_free (frame);
 
   return ret;
 }
@@ -1952,7 +2064,7 @@ gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
   while (parse->priv->buffers_pending) {
     buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
     GST_LOG_OBJECT (parse, "adding pending buffer (size %d)",
-        GST_BUFFER_SIZE (buf));
+        gst_buffer_get_size (buf));
     gst_adapter_push (parse->priv->adapter, buf);
     parse->priv->buffers_pending =
         g_slist_delete_link (parse->priv->buffers_pending,
@@ -1963,7 +2075,7 @@ gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
    * ok if taken from subclass or upstream */
   parse->priv->next_ts = GST_CLOCK_TIME_NONE;
   /* prevent it hanging around stop all the time */
-  parse->segment.last_stop = GST_CLOCK_TIME_NONE;
+  parse->segment.position = GST_CLOCK_TIME_NONE;
   /* mark next run */
   parse->priv->discont = TRUE;
 
@@ -2072,18 +2184,18 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
   const guint8 *data;
   guint old_min_size = 0, min_size, av;
   GstClockTime timestamp;
-  GstBaseParseFrame _frame = { 0, };
   GstBaseParseFrame *frame;
 
   parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
   bclass = GST_BASE_PARSE_GET_CLASS (parse);
-  frame = &_frame;
+  frame = &parse->priv->frame;
 
   if (G_LIKELY (buffer)) {
     GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
-        GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
+        gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer));
     if (G_UNLIKELY (parse->priv->passthrough)) {
-      frame->buffer = gst_buffer_make_metadata_writable (buffer);
+      gst_base_parse_frame_init (frame);
+      frame->buffer = gst_buffer_make_writable (buffer);
       return gst_base_parse_push_frame (parse, frame);
     }
     /* upstream feeding us in reverse playback;
@@ -2099,16 +2211,29 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
     gst_adapter_push (parse->priv->adapter, buffer);
   }
 
+  if (G_UNLIKELY (buffer &&
+          GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
+    frame->_private_flags |= GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
+    gst_base_parse_frame_free (frame);
+  }
+
   /* Parse and push as many frames as possible */
   /* Stop either when adapter is empty or we are flushing */
   while (!parse->priv->flushing) {
     gboolean res;
 
+    /* maintain frame state for a single frame parsing round across _chain calls,
+     * so only init when needed */
+    if (!frame->_private_flags)
+      gst_base_parse_frame_init (frame);
+
     tmpbuf = gst_buffer_new ();
 
     old_min_size = 0;
     /* Synchronization loop */
     for (;;) {
+      /* note: if subclass indicates MAX fsize,
+       * this will not likely be available anyway ... */
       min_size = MAX (parse->priv->min_frame_size, fsize);
       av = gst_adapter_available (parse->priv->adapter);
 
@@ -2135,11 +2260,11 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
       }
 
       /* always pass all available data */
-      data = gst_adapter_peek (parse->priv->adapter, av);
-      GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
-      GST_BUFFER_SIZE (tmpbuf) = min_size;
+      data = gst_adapter_map (parse->priv->adapter, av);
+      gst_buffer_take_memory (tmpbuf, -1,
+          gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
+              (gpointer) data, NULL, av, 0, av));
       GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
-      GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
 
       if (parse->priv->discont) {
         GST_DEBUG_OBJECT (parse, "marking DISCONT");
@@ -2149,6 +2274,7 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
       skip = -1;
       gst_base_parse_frame_update (parse, frame, tmpbuf);
       res = bclass->check_valid_frame (parse, frame, &fsize, &skip);
+      gst_adapter_unmap (parse->priv->adapter, 0);
       gst_buffer_replace (&frame->buffer, NULL);
       if (res) {
         if (gst_adapter_available (parse->priv->adapter) < fsize) {
@@ -2173,7 +2299,7 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
            * fragment coming later, hopefully subclass skips efficiently ... */
           timestamp = gst_adapter_prev_timestamp (parse->priv->adapter, NULL);
           outbuf = gst_adapter_take_buffer (parse->priv->adapter, skip);
-          outbuf = gst_buffer_make_metadata_writable (outbuf);
+          outbuf = gst_buffer_make_writable (outbuf);
           GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
           parse->priv->buffers_pending =
               g_slist_prepend (parse->priv->buffers_pending, outbuf);
@@ -2225,7 +2351,7 @@ gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
 
     /* FIXME: Would it be more efficient to make a subbuffer instead? */
     outbuf = gst_adapter_take_buffer (parse->priv->adapter, fsize);
-    outbuf = gst_buffer_make_metadata_writable (outbuf);
+    outbuf = gst_buffer_make_writable (outbuf);
 
     /* Subclass may want to know the data offset */
     GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
@@ -2271,11 +2397,11 @@ gst_base_parse_pull_range (GstBaseParse * parse, guint size,
    * We do it mainly to avoid pulling buffers of 1 byte all the time */
   if (parse->priv->cache) {
     gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
-    gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
+    gint cache_size = gst_buffer_get_size (parse->priv->cache);
 
     if (cache_offset <= parse->priv->offset &&
         (parse->priv->offset + size) <= (cache_offset + cache_size)) {
-      *buffer = gst_buffer_create_sub (parse->priv->cache,
+      *buffer = gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL,
           parse->priv->offset - cache_offset, size);
       GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
       return GST_FLOW_OK;
@@ -2294,8 +2420,10 @@ gst_base_parse_pull_range (GstBaseParse * parse, guint size,
     return ret;
   }
 
-  if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
-    *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
+  if (gst_buffer_get_size (parse->priv->cache) >= size) {
+    *buffer =
+        gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0,
+        size);
     GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
     return GST_FLOW_OK;
   }
@@ -2314,10 +2442,10 @@ gst_base_parse_pull_range (GstBaseParse * parse, guint size,
     return ret;
   }
 
-  if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
+  if (gst_buffer_get_size (parse->priv->cache) < size) {
     GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
         G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
-        size, GST_BUFFER_SIZE (parse->priv->cache));
+        size, gst_buffer_get_size (parse->priv->cache));
 
     *buffer = parse->priv->cache;
     parse->priv->cache = NULL;
@@ -2325,7 +2453,8 @@ gst_base_parse_pull_range (GstBaseParse * parse, guint size,
     return GST_FLOW_OK;
   }
 
-  *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
+  *buffer =
+      gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0, size);
   GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
 
   return GST_FLOW_OK;
@@ -2410,6 +2539,12 @@ gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
   GST_LOG_OBJECT (parse, "scanning for frame at offset %" G_GUINT64_FORMAT
       " (%#" G_GINT64_MODIFIER "x)", parse->priv->offset, parse->priv->offset);
 
+  /* let's make this efficient for all subclass once and for all;
+   * maybe it does not need this much, but in the latter case, we know we are
+   * in pull mode here and might as well try to read and supply more anyway
+   * (so does the buffer caching mechanism) */
+  fsize = 64 * 1024;
+
   while (TRUE) {
     gboolean res;
 
@@ -2430,7 +2565,7 @@ gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
 
     /* if we got a short read, inform subclass we are draining leftover
      * and no more is to be expected */
-    if (GST_BUFFER_SIZE (buffer) < min_size)
+    if (gst_buffer_get_size (buffer) < min_size)
       parse->priv->drain = TRUE;
 
     skip = -1;
@@ -2451,7 +2586,7 @@ gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
         /* reverse playback, and no frames found yet, so we are skipping
          * the leading part of a fragment, which may form the tail of
          * fragment coming later, hopefully subclass skips efficiently ... */
-        outbuf = gst_buffer_create_sub (buffer, 0, skip);
+        outbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 0, skip);
         parse->priv->buffers_pending =
             g_slist_prepend (parse->priv->buffers_pending, outbuf);
         outbuf = NULL;
@@ -2460,7 +2595,9 @@ gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
       if (!parse->priv->discont)
         parse->priv->sync_offset = parse->priv->offset;
       parse->priv->discont = TRUE;
-      /* something changed least; nullify loop check */
+      /* something changed at least; nullify loop check */
+      if (fsize == G_MAXUINT)
+        fsize = old_min_size + 64 * 1024;
       old_min_size = 0;
     }
     /* skip == 0 should imply subclass set min_size to need more data;
@@ -2478,8 +2615,8 @@ gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
   else if (skip < 0)
     skip = 0;
 
-  if (fsize + skip <= GST_BUFFER_SIZE (buffer)) {
-    outbuf = gst_buffer_create_sub (buffer, skip, fsize);
+  if (fsize + skip <= gst_buffer_get_size (buffer)) {
+    outbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, skip, fsize);
     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer) + skip;
     GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
     gst_buffer_unref (buffer);
@@ -2488,7 +2625,7 @@ gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
     ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
     if (ret != GST_FLOW_OK)
       goto done;
-    if (GST_BUFFER_SIZE (outbuf) < fsize) {
+    if (gst_buffer_get_size (outbuf) < fsize) {
       gst_buffer_unref (outbuf);
       ret = GST_FLOW_UNEXPECTED;
     }
@@ -2518,7 +2655,7 @@ gst_base_parse_loop (GstPad * pad)
   GstBaseParse *parse;
   GstBaseParseClass *klass;
   GstFlowReturn ret = GST_FLOW_OK;
-  GstBaseParseFrame frame = { 0, };
+  GstBaseParseFrame frame;
 
   parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
   klass = GST_BASE_PARSE_GET_CLASS (parse);
@@ -2535,6 +2672,7 @@ gst_base_parse_loop (GstPad * pad)
     }
   }
 
+  gst_base_parse_frame_init (&frame);
   ret = gst_base_parse_scan_frame (parse, klass, &frame, TRUE);
   if (ret != GST_FLOW_OK)
     goto done;
@@ -2544,7 +2682,7 @@ gst_base_parse_loop (GstPad * pad)
 
   /* eat expected eos signalling past segment in reverse playback */
   if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
-      parse->segment.last_stop >= parse->segment.stop) {
+      parse->segment.position >= parse->segment.stop) {
     GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
     /* push what was accumulated during loop run */
     gst_base_parse_process_fragment (parse, TRUE);
@@ -2624,12 +2762,24 @@ gst_base_parse_sink_activate (GstPad * sinkpad)
 {
   GstBaseParse *parse;
   gboolean result = TRUE;
+  GstQuery *query;
+  gboolean pull_mode;
 
   parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
 
   GST_DEBUG_OBJECT (parse, "sink activate");
 
-  if (gst_pad_check_pull_range (sinkpad)) {
+  query = gst_query_new_scheduling ();
+  result = gst_pad_peer_query (sinkpad, query);
+  if (result) {
+    gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL,
+        NULL);
+  } else {
+    pull_mode = FALSE;
+  }
+  gst_query_unref (query);
+
+  if (pull_mode) {
     GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
     result = gst_pad_activate_pull (sinkpad, TRUE);
   } else {
@@ -2705,9 +2855,7 @@ gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
 
   if (result) {
     if (active) {
-      parse->priv->pending_segment = gst_event_new_new_segment (FALSE,
-          parse->segment.rate, parse->segment.format,
-          parse->segment.start, parse->segment.stop, parse->segment.last_stop);
+      parse->priv->pending_segment = gst_event_new_segment (&parse->segment);
       result &=
           gst_pad_start_task (sinkpad, (GstTaskFunction) gst_base_parse_loop,
           sinkpad);
@@ -2785,8 +2933,8 @@ exit:
  *
  * By default, announced average bitrate is estimated. The average bitrate
  * is used to estimate the total duration of the stream and to estimate
- * a seek position, if there's no index and #GST_BASE_PARSE_FORMAT_FLAG_SYNCABLE
- * is set.
+ * a seek position, if there's no index and the format is syncable
+ * (see gst_base_parse_set_syncable()).
  *
  * Since: 0.10.33
  */
@@ -2911,7 +3059,7 @@ gst_base_parse_set_syncable (GstBaseParse * parse, gboolean syncable)
 /**
  * gst_base_parse_set_passthrough:
  * @parse: a #GstBaseParse
- * @passthrough:
+ * @passthrough: %TRUE if parser should run in passthrough mode
  *
  * Set if the nature of the format or configuration does not allow (much)
  * parsing, and the parser should operate in passthrough mode (which only
@@ -2930,6 +3078,31 @@ gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
   GST_INFO_OBJECT (parse, "passthrough: %s", (passthrough) ? "yes" : "no");
 }
 
+/**
+ * gst_base_parse_set_latency:
+ * @parse: a #GstBaseParse
+ * @min_latency: minimum parse latency
+ * @max_latency: maximum parse latency
+ *
+ * Sets the minimum and maximum (which may likely be equal) latency introduced
+ * by the parsing process.  If there is such a latency, which depends on the
+ * particular parsing of the format, it typically corresponds to 1 frame duration.
+ *
+ * Since: 0.10.34
+ */
+void
+gst_base_parse_set_latency (GstBaseParse * parse, GstClockTime min_latency,
+    GstClockTime max_latency)
+{
+  GST_OBJECT_LOCK (parse);
+  parse->priv->min_latency = min_latency;
+  parse->priv->max_latency = max_latency;
+  GST_OBJECT_UNLOCK (parse);
+  GST_INFO_OBJECT (parse, "min/max latency %" GST_TIME_FORMAT ", %"
+      GST_TIME_FORMAT, GST_TIME_ARGS (min_latency),
+      GST_TIME_ARGS (max_latency));
+}
+
 static gboolean
 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
     GstClockTime * duration)
@@ -2997,8 +3170,8 @@ gst_base_parse_query (GstPad * pad, GstQuery * query)
         dest_value = parse->priv->offset;
         res = TRUE;
       } else if (format == parse->segment.format &&
-          GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
-        dest_value = parse->segment.last_stop;
+          GST_CLOCK_TIME_IS_VALID (parse->segment.position)) {
+        dest_value = parse->segment.position;
         res = TRUE;
       }
       GST_OBJECT_UNLOCK (parse);
@@ -3090,6 +3263,29 @@ gst_base_parse_query (GstPad * pad, GstQuery * query)
       }
       break;
     }
+    case GST_QUERY_LATENCY:
+    {
+      if ((res = gst_pad_peer_query (parse->sinkpad, query))) {
+        gboolean live;
+        GstClockTime min_latency, max_latency;
+
+        gst_query_parse_latency (query, &live, &min_latency, &max_latency);
+        GST_DEBUG_OBJECT (parse, "Peer latency: live %d, min %"
+            GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
+            GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
+
+        GST_OBJECT_LOCK (parse);
+        /* add our latency */
+        if (min_latency != -1)
+          min_latency += parse->priv->min_latency;
+        if (max_latency != -1)
+          max_latency += parse->priv->max_latency;
+        GST_OBJECT_UNLOCK (parse);
+
+        gst_query_set_latency (query, live, min_latency, max_latency);
+      }
+      break;
+    }
     default:
       res = gst_pad_query_default (pad, query);
       break;
@@ -3108,11 +3304,11 @@ gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
   gboolean orig_drain, orig_discont;
   GstFlowReturn ret = GST_FLOW_OK;
   GstBuffer *buf = NULL;
-  GstBaseParseFrame frame = { 0, };
+  GstBaseParseFrame frame;
 
-  g_return_val_if_fail (GST_FLOW_ERROR, pos != NULL);
-  g_return_val_if_fail (GST_FLOW_ERROR, time != NULL);
-  g_return_val_if_fail (GST_FLOW_ERROR, duration != NULL);
+  g_return_val_if_fail (pos != NULL, GST_FLOW_ERROR);
+  g_return_val_if_fail (time != NULL, GST_FLOW_ERROR);
+  g_return_val_if_fail (duration != NULL, GST_FLOW_ERROR);
 
   klass = GST_BASE_PARSE_GET_CLASS (parse);
 
@@ -3127,6 +3323,8 @@ gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
   GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
       " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
 
+  gst_base_parse_frame_init (&frame);
+
   /* jump elsewhere and locate next frame */
   parse->priv->offset = *pos;
   ret = gst_base_parse_scan_frame (parse, klass, &frame, FALSE);
@@ -3137,7 +3335,8 @@ gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
   GST_LOG_OBJECT (parse,
       "peek parsing frame at offset %" G_GUINT64_FORMAT
       " (%#" G_GINT64_MODIFIER "x) of size %d",
-      GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf));
+      GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf),
+      gst_buffer_get_size (buf));
 
   /* get offset first, subclass parsing might dump other stuff in there */
   *pos = GST_BUFFER_OFFSET (buf);
@@ -3147,7 +3346,8 @@ gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
   /* but it should provide proper time */
   *time = GST_BUFFER_TIMESTAMP (buf);
   *duration = GST_BUFFER_DURATION (buf);
-  gst_base_parse_frame_clear (parse, &frame);
+
+  gst_base_parse_frame_free (&frame);
 
   GST_LOG_OBJECT (parse,
       "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
@@ -3312,7 +3512,7 @@ gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
     goto exit;
   }
 
-  GST_OBJECT_LOCK (parse);
+  g_static_mutex_lock (&parse->priv->index_lock);
   if (parse->priv->index) {
     /* Let's check if we have an index entry for that time */
     entry = gst_index_get_assoc_entry (parse->priv->index,
@@ -3336,7 +3536,7 @@ gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
       ts = GST_CLOCK_TIME_NONE;
     }
   }
-  GST_OBJECT_UNLOCK (parse);
+  g_static_mutex_unlock (&parse->priv->index_lock);
 
 exit:
   if (_ts)
@@ -3394,10 +3594,10 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
 
   /* copy segment, we need this because we still need the old
    * segment when we close the current segment. */
-  memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
+  gst_segment_copy_into (&parse->segment, &seeksegment);
 
   GST_DEBUG_OBJECT (parse, "configuring seek");
-  gst_segment_set_seek (&seeksegment, rate, format, flags,
+  gst_segment_do_seek (&seeksegment, rate, format, flags,
       cur_type, cur, stop_type, stop, &update);
 
   /* accurate seeking implies seek tables are used to obtain position,
@@ -3405,13 +3605,13 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
   accurate = flags & GST_SEEK_FLAG_ACCURATE;
 
   /* maybe we can be accurate for (almost) free */
-  gst_base_parse_find_offset (parse, seeksegment.last_stop, TRUE, &start_ts);
-  if (seeksegment.last_stop <= start_ts + TARGET_DIFFERENCE) {
+  gst_base_parse_find_offset (parse, seeksegment.position, TRUE, &start_ts);
+  if (seeksegment.position <= start_ts + TARGET_DIFFERENCE) {
     GST_DEBUG_OBJECT (parse, "accurate seek possible");
     accurate = TRUE;
   }
   if (accurate) {
-    GstClockTime startpos = seeksegment.last_stop;
+    GstClockTime startpos = seeksegment.position;
 
     /* accurate requested, so ... seek a bit before target */
     if (startpos < parse->priv->lead_in_ts)
@@ -3422,9 +3622,9 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
     seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
         NULL);
   } else {
-    start_ts = seeksegment.last_stop;
+    start_ts = seeksegment.position;
     dstformat = GST_FORMAT_BYTES;
-    if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
+    if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.position,
             &dstformat, &seekpos))
       goto convert_failed;
     if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
@@ -3460,7 +3660,7 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
     GST_PAD_STREAM_LOCK (parse->sinkpad);
 
     /* save current position */
-    last_stop = parse->segment.last_stop;
+    last_stop = parse->segment.position;
     GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
         last_stop);
 
@@ -3469,26 +3669,13 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
     /* prepare for streaming again */
     if (flush) {
       GST_DEBUG_OBJECT (parse, "sending flush stop");
-      gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
-      gst_pad_push_event (parse->sinkpad, gst_event_new_flush_stop ());
+      gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop (TRUE));
+      gst_pad_push_event (parse->sinkpad, gst_event_new_flush_stop (TRUE));
       gst_base_parse_clear_queues (parse);
     } else {
-      if (parse->priv->close_segment)
-        gst_event_unref (parse->priv->close_segment);
-
-      parse->priv->close_segment = gst_event_new_new_segment (TRUE,
-          parse->segment.rate, parse->segment.format,
-          parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
-
-      /* keep track of our last_stop */
-      seeksegment.accum = parse->segment.last_stop;
-
-      GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
-          "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
-          ", pos = %" GST_TIME_FORMAT, format,
-          GST_TIME_ARGS (parse->segment.accum),
-          GST_TIME_ARGS (parse->segment.last_stop),
-          GST_TIME_ARGS (parse->segment.accum));
+      /* keep track of our position */
+      seeksegment.base = gst_segment_to_running_time (&seeksegment,
+          seeksegment.format, parse->segment.position);
     }
 
     memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
@@ -3498,10 +3685,7 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
       gst_event_unref (parse->priv->pending_segment);
 
     /* This will be sent later in _loop() */
-    parse->priv->pending_segment =
-        gst_event_new_new_segment (FALSE, parse->segment.rate,
-        parse->segment.format, parse->segment.start,
-        parse->segment.stop, parse->segment.start);
+    parse->priv->pending_segment = gst_event_new_segment (&parse->segment);
 
     GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
         "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
@@ -3514,7 +3698,7 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
      * maybe scan and subclass can find where to go */
     if (!accurate) {
       gint64 scanpos;
-      GstClockTime ts = seeksegment.last_stop;
+      GstClockTime ts = seeksegment.position;
 
       gst_base_parse_locate_time (parse, &ts, &scanpos);
       if (scanpos >= 0) {
@@ -3558,7 +3742,7 @@ gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
        seek event (in bytes) to upstream. Segment / flush handling happens
        in corresponding src event handlers */
     GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
-    if (seekstop >= 0 && seekpos <= seekpos)
+    if (seekstop >= 0 && seekstop <= seekpos)
       seekstop = seekpos;
     new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
         GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
@@ -3641,40 +3825,23 @@ gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
   }
 }
 
-static gboolean
-gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
-{
-  GstBaseParse *parse;
-  GstBaseParseClass *klass;
-  gboolean res = TRUE;
-
-  parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
-  klass = GST_BASE_PARSE_GET_CLASS (parse);
-
-  GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
-
-  if (klass->set_sink_caps)
-    res = klass->set_sink_caps (parse, caps);
-
-  return res;
-}
-
 static void
 gst_base_parse_set_index (GstElement * element, GstIndex * index)
 {
   GstBaseParse *parse = GST_BASE_PARSE (element);
 
-  GST_OBJECT_LOCK (parse);
+  g_static_mutex_lock (&parse->priv->index_lock);
   if (parse->priv->index)
     gst_object_unref (parse->priv->index);
   if (index) {
     parse->priv->index = gst_object_ref (index);
-    gst_index_get_writer_id (index, GST_OBJECT (element),
+    gst_index_get_writer_id (index, GST_OBJECT_CAST (element),
         &parse->priv->index_id);
     parse->priv->own_index = FALSE;
-  } else
+  } else {
     parse->priv->index = NULL;
-  GST_OBJECT_UNLOCK (parse);
+  }
+  g_static_mutex_unlock (&parse->priv->index_lock);
 }
 
 static GstIndex *
@@ -3683,10 +3850,10 @@ gst_base_parse_get_index (GstElement * element)
   GstBaseParse *parse = GST_BASE_PARSE (element);
   GstIndex *result = NULL;
 
-  GST_OBJECT_LOCK (parse);
+  g_static_mutex_lock (&parse->priv->index_lock);
   if (parse->priv->index)
     result = gst_object_ref (parse->priv->index);
-  GST_OBJECT_UNLOCK (parse);
+  g_static_mutex_unlock (&parse->priv->index_lock);
 
   return result;
 }
@@ -3703,6 +3870,7 @@ gst_base_parse_change_state (GstElement * element, GstStateChange transition)
     case GST_STATE_CHANGE_READY_TO_PAUSED:
       /* If this is our own index destroy it as the
        * old entries might be wrong for the new stream */
+      g_static_mutex_lock (&parse->priv->index_lock);
       if (parse->priv->own_index) {
         gst_object_unref (parse->priv->index);
         parse->priv->index = NULL;
@@ -3718,6 +3886,7 @@ gst_base_parse_change_state (GstElement * element, GstStateChange transition)
             &parse->priv->index_id);
         parse->priv->own_index = TRUE;
       }
+      g_static_mutex_unlock (&parse->priv->index_lock);
       break;
     default:
       break;