audioparsers: use new base parse API to fix tag handling
[platform/upstream/gst-plugins-good.git] / gst / audioparsers / gstdcaparse.c
index 7c11e0b..cfe97e1 100644 (file)
@@ -13,8 +13,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
@@ -27,7 +27,7 @@
  * <refsect2>
  * <title>Example launch line</title>
  * |[
- * gst-launch filesrc location=abc.dts ! dcaparse ! dtsdec ! audioresample ! audioconvert ! autoaudiosink
+ * gst-launch-1.0 filesrc location=abc.dts ! dcaparse ! dtsdec ! audioresample ! audioconvert ! autoaudiosink
  * ]|
  * </refsect2>
  */
@@ -48,8 +48,8 @@
 #include <string.h>
 
 #include "gstdcaparse.h"
-#include <gst/base/gstbytereader.h>
-#include <gst/base/gstbitreader.h>
+#include <gst/base/base.h>
+#include <gst/pbutils/pbutils.h>
 
 GST_DEBUG_CATEGORY_STATIC (dca_parse_debug);
 #define GST_CAT_DEFAULT dca_parse_debug
@@ -68,18 +68,20 @@ static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("audio/x-dts"));
+    GST_STATIC_CAPS ("audio/x-dts; " "audio/x-private1-dts"));
 
 static void gst_dca_parse_finalize (GObject * object);
 
 static gboolean gst_dca_parse_start (GstBaseParse * parse);
 static gboolean gst_dca_parse_stop (GstBaseParse * parse);
-static gboolean gst_dca_parse_check_valid_frame (GstBaseParse * parse,
-    GstBaseParseFrame * frame, guint * size, gint * skipsize);
-static GstFlowReturn gst_dca_parse_parse_frame (GstBaseParse * parse,
+static GstFlowReturn gst_dca_parse_handle_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame, gint * skipsize);
+static GstFlowReturn gst_dca_parse_pre_push_frame (GstBaseParse * parse,
     GstBaseParseFrame * frame);
 static GstCaps *gst_dca_parse_get_sink_caps (GstBaseParse * parse,
     GstCaps * filter);
+static gboolean gst_dca_parse_set_sink_caps (GstBaseParse * parse,
+    GstCaps * caps);
 
 #define gst_dca_parse_parent_class parent_class
 G_DEFINE_TYPE (GstDcaParse, gst_dca_parse, GST_TYPE_BASE_PARSE);
@@ -98,17 +100,18 @@ gst_dca_parse_class_init (GstDcaParseClass * klass)
 
   parse_class->start = GST_DEBUG_FUNCPTR (gst_dca_parse_start);
   parse_class->stop = GST_DEBUG_FUNCPTR (gst_dca_parse_stop);
-  parse_class->check_valid_frame =
-      GST_DEBUG_FUNCPTR (gst_dca_parse_check_valid_frame);
-  parse_class->parse_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_parse_frame);
+  parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_dca_parse_handle_frame);
+  parse_class->pre_push_frame =
+      GST_DEBUG_FUNCPTR (gst_dca_parse_pre_push_frame);
   parse_class->get_sink_caps = GST_DEBUG_FUNCPTR (gst_dca_parse_get_sink_caps);
+  parse_class->set_sink_caps = GST_DEBUG_FUNCPTR (gst_dca_parse_set_sink_caps);
 
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&sink_template));
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&src_template));
 
-  gst_element_class_set_details_simple (element_class,
+  gst_element_class_set_static_metadata (element_class,
       "DTS Coherent Acoustics audio stream parser", "Codec/Parser/Audio",
       "DCA parser", "Tim-Philipp Müller <tim centricular net>");
 }
@@ -123,6 +126,7 @@ gst_dca_parse_reset (GstDcaParse * dcaparse)
   dcaparse->block_size = -1;
   dcaparse->frame_size = -1;
   dcaparse->last_sync = 0;
+  dcaparse->sent_codec_tag = FALSE;
 }
 
 static void
@@ -131,6 +135,11 @@ gst_dca_parse_init (GstDcaParse * dcaparse)
   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (dcaparse),
       DCA_MIN_FRAMESIZE);
   gst_dca_parse_reset (dcaparse);
+  dcaparse->baseparse_chainfunc =
+      GST_BASE_PARSE_SINK_PAD (GST_BASE_PARSE (dcaparse))->chainfunc;
+
+  GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (dcaparse));
+  GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (dcaparse));
 }
 
 static void
@@ -305,44 +314,46 @@ gst_dca_parse_find_sync (GstDcaParse * dcaparse, GstByteReader * reader,
   return best_offset;
 }
 
-static gboolean
-gst_dca_parse_check_valid_frame (GstBaseParse * parse,
-    GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
+static GstFlowReturn
+gst_dca_parse_handle_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame, gint * skipsize)
 {
   GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
   GstBuffer *buf = frame->buffer;
   GstByteReader r;
-  gboolean parser_draining;
   gboolean parser_in_sync;
   gboolean terminator;
   guint32 sync = 0;
-  guint size, rate, chans, num_blocks, samples_per_block;
+  guint size = 0, rate, chans, num_blocks, samples_per_block, depth;
+  gint block_size;
+  gint endianness;
   gint off = -1;
-  gpointer data;
-  gsize bufsize;
-  gboolean ret = FALSE;
+  GstMapInfo map;
+  GstFlowReturn ret = GST_FLOW_EOS;
 
-  data = gst_buffer_map (buf, &bufsize, NULL, GST_MAP_READ);
+  gst_buffer_map (buf, &map, GST_MAP_READ);
 
-  if (G_UNLIKELY (bufsize < 16))
+  if (G_UNLIKELY (map.size < 16)) {
+    *skipsize = 1;
     goto cleanup;
+  }
 
   parser_in_sync = !GST_BASE_PARSE_LOST_SYNC (parse);
 
-  gst_byte_reader_init (&r, data, bufsize);
+  gst_byte_reader_init (&r, map.data, map.size);
 
   if (G_LIKELY (parser_in_sync && dcaparse->last_sync != 0)) {
     off = gst_byte_reader_masked_scan_uint32 (&r, 0xffffffff,
-        dcaparse->last_sync, 0, size);
+        dcaparse->last_sync, 0, map.size);
   }
 
   if (G_UNLIKELY (off < 0)) {
-    off = gst_dca_parse_find_sync (dcaparse, &r, bufsize, &sync);
+    off = gst_dca_parse_find_sync (dcaparse, &r, map.size, &sync);
   }
 
   /* didn't find anything that looks like a sync word, skip */
   if (off < 0) {
-    *skipsize = bufsize - 3;
+    *skipsize = map.size - 3;
     GST_DEBUG_OBJECT (dcaparse, "no sync, skipping %d bytes", *skipsize);
     goto cleanup;
   }
@@ -356,8 +367,8 @@ gst_dca_parse_check_valid_frame (GstBaseParse * parse,
   }
 
   /* make sure the values in the frame header look sane */
-  if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, NULL,
-          NULL, &num_blocks, &samples_per_block, &terminator)) {
+  if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth,
+          &endianness, &num_blocks, &samples_per_block, &terminator)) {
     *skipsize = 4;
     goto cleanup;
   }
@@ -365,21 +376,25 @@ gst_dca_parse_check_valid_frame (GstBaseParse * parse,
   GST_LOG_OBJECT (parse, "got frame, sync %08x, size %u, rate %d, channels %d",
       sync, size, rate, chans);
 
-  *framesize = size;
-
   dcaparse->last_sync = sync;
 
+  /* FIXME: Don't look for a second syncword, there are streams out there
+   * that consistently contain garbage between every frame so we never ever
+   * find a second consecutive syncword.
+   * See https://bugzilla.gnome.org/show_bug.cgi?id=738237
+   */
+#if 0
   parser_draining = GST_BASE_PARSE_DRAINING (parse);
 
   if (!parser_in_sync && !parser_draining) {
     /* check for second frame to be sure */
     GST_DEBUG_OBJECT (dcaparse, "resyncing; checking next frame syncword");
-    if (bufsize >= (size + 16)) {
+    if (map.size >= (size + 16)) {
       guint s2, r2, c2, n2, s3;
       gboolean t;
 
-      GST_MEMDUMP ("buf", data, size + 16);
-      gst_byte_reader_init (&r, data, bufsize);
+      GST_MEMDUMP ("buf", map.data, size + 16);
+      gst_byte_reader_init (&r, map.data, map.size);
       gst_byte_reader_skip_unchecked (&r, size);
 
       if (!gst_dca_parse_parse_header (dcaparse, &r, &s2, &r2, &c2, NULL, NULL,
@@ -392,42 +407,19 @@ gst_dca_parse_check_valid_frame (GstBaseParse * parse,
       /* ok, got sync now, let's assume constant frame size */
       gst_base_parse_set_min_frame_size (parse, size);
     } else {
-      /* FIXME: baseparse always seems to hand us buffers of min_frame_size
-       * bytes, which is unhelpful here */
+      /* wait for some more data */
       GST_LOG_OBJECT (dcaparse,
-          "next sync out of reach (%" G_GSIZE_FORMAT " < %u)", bufsize,
+          "next sync out of reach (%" G_GSIZE_FORMAT " < %u)", map.size,
           size + 16);
-      /* *skipsize = 0; */
-      /* return FALSE; */
+      goto cleanup;
     }
   }
+#endif
 
-  ret = TRUE;
-
-cleanup:
-  gst_buffer_unmap (buf, data, bufsize);
-  return ret;
-}
-
-static GstFlowReturn
-gst_dca_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
-{
-  GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
-  GstBuffer *buf = frame->buffer;
-  GstByteReader r;
-  guint size, rate, chans, depth, block_size, num_blocks, samples_per_block;
-  gint endianness;
-  gboolean terminator;
-  gpointer data;
-  gsize bufsize;
-
-  data = gst_buffer_map (buf, &bufsize, NULL, GST_MAP_READ);
-  gst_byte_reader_init (&r, data, bufsize);
-
-  if (!gst_dca_parse_parse_header (dcaparse, &r, &size, &rate, &chans, &depth,
-          &endianness, &num_blocks, &samples_per_block, &terminator))
-    goto broken_header;
+  /* found frame */
+  ret = GST_FLOW_OK;
 
+  /* metadata handling */
   block_size = num_blocks * samples_per_block;
 
   if (G_UNLIKELY (dcaparse->rate != rate || dcaparse->channels != chans
@@ -455,50 +447,138 @@ gst_dca_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
     gst_base_parse_set_frame_rate (parse, rate, block_size, 0, 0);
   }
 
-  gst_buffer_unmap (buf, data, bufsize);
-  return GST_FLOW_OK;
+cleanup:
+  gst_buffer_unmap (buf, &map);
 
-/* ERRORS */
-broken_header:
-  {
-    /* this really shouldn't ever happen */
-    GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
-    gst_buffer_unmap (buf, data, bufsize);
-    return GST_FLOW_ERROR;
+  if (ret == GST_FLOW_OK && size <= map.size) {
+    ret = gst_base_parse_finish_frame (parse, frame, size);
+  } else {
+    ret = GST_FLOW_OK;
+  }
+
+  return ret;
+}
+
+/*
+ * MPEG-PS private1 streams add a 2 bytes "Audio Substream Headers" for each
+ * buffer (not each frame) with the offset of the next frame's start.
+ * These 2 bytes can be dropped safely as they do not include any timing
+ * information, only the offset to the start of the next frame.
+ * See gstac3parse.c for a more detailed description.
+ * */
+
+static GstFlowReturn
+gst_dca_parse_chain_priv (GstPad * pad, GstObject * parent, GstBuffer * buffer)
+{
+  GstDcaParse *dcaparse = GST_DCA_PARSE (parent);
+  GstFlowReturn ret;
+  GstBuffer *newbuf;
+  gsize size;
+
+  size = gst_buffer_get_size (buffer);
+  if (size >= 2) {
+    newbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, 2, size - 2);
+    gst_buffer_unref (buffer);
+    ret = dcaparse->baseparse_chainfunc (pad, parent, newbuf);
+  } else {
+    gst_buffer_unref (buffer);
+    ret = GST_FLOW_OK;
+  }
+
+  return ret;
+}
+
+static void
+remove_fields (GstCaps * caps)
+{
+  guint i, n;
+
+  n = gst_caps_get_size (caps);
+  for (i = 0; i < n; i++) {
+    GstStructure *s = gst_caps_get_structure (caps, i);
+
+    gst_structure_remove_field (s, "framed");
   }
 }
 
 static GstCaps *
 gst_dca_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
 {
-  GstCaps *peercaps;
+  GstCaps *peercaps, *templ;
   GstCaps *res;
 
-  /* FIXME: handle filter caps */
+  templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
+  if (filter) {
+    GstCaps *fcopy = gst_caps_copy (filter);
+    /* Remove the fields we convert */
+    remove_fields (fcopy);
+    peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
+    gst_caps_unref (fcopy);
+  } else
+    peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
 
-  peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
   if (peercaps) {
-    guint i, n;
-
     /* Remove the framed field */
     peercaps = gst_caps_make_writable (peercaps);
-    n = gst_caps_get_size (peercaps);
-    for (i = 0; i < n; i++) {
-      GstStructure *s = gst_caps_get_structure (peercaps, i);
-
-      gst_structure_remove_field (s, "framed");
-    }
+    remove_fields (peercaps);
 
-    res =
-        gst_caps_intersect_full (peercaps,
-        gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
-        GST_CAPS_INTERSECT_FIRST);
+    res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
     gst_caps_unref (peercaps);
+    gst_caps_unref (templ);
   } else {
-    res =
-        gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
-            (parse)));
+    res = templ;
+  }
+
+  if (filter) {
+    GstCaps *intersection;
+
+    intersection =
+        gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (res);
+    res = intersection;
   }
 
   return res;
 }
+
+static gboolean
+gst_dca_parse_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
+{
+  GstStructure *s;
+  GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
+
+  s = gst_caps_get_structure (caps, 0);
+  if (gst_structure_has_name (s, "audio/x-private1-dts")) {
+    gst_pad_set_chain_function (parse->sinkpad, gst_dca_parse_chain_priv);
+  } else {
+    gst_pad_set_chain_function (parse->sinkpad, dcaparse->baseparse_chainfunc);
+  }
+  return TRUE;
+}
+
+static GstFlowReturn
+gst_dca_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
+{
+  GstDcaParse *dcaparse = GST_DCA_PARSE (parse);
+
+  if (!dcaparse->sent_codec_tag) {
+    GstTagList *taglist;
+    GstCaps *caps;
+
+    taglist = gst_tag_list_new_empty ();
+
+    /* codec tag */
+    caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
+    gst_pb_utils_add_codec_description_to_tag_list (taglist,
+        GST_TAG_AUDIO_CODEC, caps);
+    gst_caps_unref (caps);
+
+    gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
+    gst_tag_list_unref (taglist);
+
+    /* also signals the end of first-frame processing */
+    dcaparse->sent_codec_tag = TRUE;
+  }
+
+  return GST_FLOW_OK;
+}