typefinding: add parsed=true to mp3 and mpeg2 video ES caps
[platform/upstream/gstreamer.git] / gst / typefind / gsttypefindfunctions.c
index df04901..a38f84d 100644 (file)
@@ -845,6 +845,90 @@ flac_type_find (GstTypeFind * tf, gpointer unused)
 #endif
 }
 
+/* TODO: we could probably make a generic function for this.. */
+static gint
+aac_type_find_scan_loas_frames_ep (GstTypeFind * tf, DataScanCtx * scan_ctx,
+    gint max_frames)
+{
+  DataScanCtx c = *scan_ctx;
+  guint16 snc;
+  guint len;
+  gint count = 0;
+
+  do {
+    if (!data_scan_ctx_ensure_data (tf, &c, 5))
+      break;
+
+    /* EPAudioSyncStream */
+    len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
+        ((c.data[4] & 0x80) >> 7);
+
+    if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
+      GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
+      break;
+    }
+
+    /* check length of frame  */
+    snc = GST_READ_UINT16_BE (c.data + len);
+    if (snc != 0x4de1) {
+      GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
+      break;
+    }
+
+    ++count;
+
+    GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
+        "framelen %u", count, c.offset, len);
+
+    data_scan_ctx_advance (tf, &c, len);
+  } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
+
+  GST_DEBUG ("found %d consecutive frames", count);
+  return count;
+}
+
+static gint
+aac_type_find_scan_loas_frames (GstTypeFind * tf, DataScanCtx * scan_ctx,
+    gint max_frames)
+{
+  DataScanCtx c = *scan_ctx;
+  guint16 snc;
+  guint len;
+  gint count = 0;
+
+  do {
+    if (!data_scan_ctx_ensure_data (tf, &c, 3))
+      break;
+
+    /* AudioSyncStream */
+    len = ((c.data[1] & 0x1f) << 8) | c.data[2];
+    /* add size of sync stream header */
+    len += 3;
+
+    if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len)) {
+      GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
+      break;
+    }
+
+    /* check length of frame  */
+    snc = GST_READ_UINT16_BE (c.data + len);
+    if ((snc & 0xffe0) != 0x56e0) {
+      GST_DEBUG ("No sync found at 0x%" G_GINT64_MODIFIER "x", c.offset + len);
+      break;
+    }
+
+    ++count;
+
+    GST_DEBUG ("Found LOAS syncword #%d at offset 0x%" G_GINT64_MODIFIER "x, "
+        "framelen %u", count, c.offset, len);
+
+    data_scan_ctx_advance (tf, &c, len);
+  } while (count < max_frames && (c.offset - scan_ctx->offset) < 64 * 1024);
+
+  GST_DEBUG ("found %d consecutive frames", count);
+  return count;
+}
+
 /*** audio/mpeg version 2, 4 ***/
 
 static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
@@ -854,8 +938,10 @@ static GstStaticCaps aac_caps = GST_STATIC_CAPS ("audio/mpeg, "
 static void
 aac_type_find (GstTypeFind * tf, gpointer unused)
 {
-  /* LUT to convert the AudioObjectType from the ADTS header to a string */
   DataScanCtx c = { 0, NULL, 0 };
+  GstTypeFindProbability best_probability = GST_TYPE_FIND_NONE;
+  GstCaps *best_caps = NULL;
+  guint best_count = 0;
 
   while (c.offset < AAC_AMOUNT) {
     guint snc, len;
@@ -941,45 +1027,29 @@ aac_type_find (GstTypeFind * tf, gpointer unused)
       }
 
       GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
-    } else if (G_UNLIKELY (((snc & 0xffe0) == 0x56e0) || (snc == 0x4de1))) {
-      /* LOAS frame */
-
-      GST_DEBUG ("Found one LOAS syncword at offset 0x%" G_GINT64_MODIFIER
-          "x, tracing next...", c.offset);
-
-      /* check length of frame for each type of detectable LOAS streams */
-      if (snc == 0x4de1) {
-        /* EPAudioSyncStream */
-        len = ((c.data[2] & 0x0f) << 9) | (c.data[3] << 1) |
-            ((c.data[4] & 0x80) >> 7);
-        /* add size of EP sync stream header */
-        len += 7;
-      } else {
-        /* AudioSyncStream */
-        len = ((c.data[1] & 0x1f) << 8) | c.data[2];
-        /* add size of sync stream header */
-        len += 3;
-      }
+    } else if (G_UNLIKELY ((snc & 0xffe0) == 0x56e0 || snc == 0x4de1)) {
+      gint count;
 
-      if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 2)) {
-        GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len);
-        goto next;
-      }
+      /* LOAS frame */
+      GST_INFO ("Possible LOAS syncword at offset 0x%" G_GINT64_MODIFIER
+          "x, scanning for more frames...", c.offset);
 
-      /* check if there's a second LOAS frame */
-      snc = GST_READ_UINT16_BE (c.data + len);
-      if (((snc & 0xffe0) == 0x56e0) || (snc == 0x4de1)) {
-        GST_DEBUG ("Found second LOAS syncword at offset 0x%"
-            G_GINT64_MODIFIER "x, framelen %u", c.offset, len);
+      if (snc == 0x4de1)
+        count = aac_type_find_scan_loas_frames_ep (tf, &c, 20);
+      else
+        count = aac_type_find_scan_loas_frames (tf, &c, 20);
 
-        gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
+      if (count >= 3 && count > best_count) {
+        gst_caps_replace (&best_caps, NULL);
+        best_caps = gst_caps_new_simple ("audio/mpeg",
             "framed", G_TYPE_BOOLEAN, FALSE,
             "mpegversion", G_TYPE_INT, 4,
             "stream-format", G_TYPE_STRING, "loas", NULL);
-        break;
+        best_count = count;
+        best_probability = GST_TYPE_FIND_POSSIBLE - 10 + count * 3;
+        if (best_probability >= GST_TYPE_FIND_LIKELY)
+          break;
       }
-
-      GST_DEBUG ("No next frame found... (should have been at 0x%x)", len);
     } else if (!memcmp (c.data, "ADIF", 4)) {
       /* ADIF header */
       gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg",
@@ -992,6 +1062,11 @@ aac_type_find (GstTypeFind * tf, gpointer unused)
 
     data_scan_ctx_advance (tf, &c, 1);
   }
+
+  if (best_probability > GST_TYPE_FIND_NONE) {
+    gst_type_find_suggest (tf, best_probability, best_caps);
+    gst_caps_unref (best_caps);
+  }
 }
 
 /*** audio/mpeg version 1 ***/
@@ -1345,7 +1420,8 @@ suggest:
     g_return_if_fail (layer >= 1 && layer <= 3);
 
     gst_type_find_suggest_simple (tf, prob, "audio/mpeg",
-        "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer, NULL);
+        "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, layer,
+        "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
   }
 }
 
@@ -1678,9 +1754,11 @@ GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false");
 static void
 wavpack_type_find (GstTypeFind * tf, gpointer unused)
 {
+  GstTypeFindProbability base_prob = GST_TYPE_FIND_POSSIBLE;
   guint64 offset;
   guint32 blocksize;
   const guint8 *data;
+  guint count_wv, count_wvc;
 
   data = gst_type_find_peek (tf, 0, 32);
   if (!data)
@@ -1695,8 +1773,10 @@ wavpack_type_find (GstTypeFind * tf, gpointer unused)
    * work in pull-mode */
   blocksize = GST_READ_UINT32_LE (data + 4);
   GST_LOG ("wavpack header, blocksize=0x%04x", blocksize);
+  count_wv = 0;
+  count_wvc = 0;
   offset = 32;
-  while (offset < 32 + blocksize) {
+  while (offset < 8 + blocksize) {
     guint32 sublen;
 
     /* get chunk header */
@@ -1711,7 +1791,7 @@ wavpack_type_find (GstTypeFind * tf, gpointer unused)
     } else {
       sublen += 1 + 1;          /* id + length */
     }
-    if (sublen > blocksize - offset + 32) {
+    if (offset + sublen > 8 + blocksize) {
       GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen,
           blocksize - offset);
       break;
@@ -1720,18 +1800,38 @@ wavpack_type_find (GstTypeFind * tf, gpointer unused)
       switch (data[0] & 0x0f) {
         case 0xa:              /* ID_WV_BITSTREAM  */
         case 0xc:              /* ID_WVX_BITSTREAM */
-          gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, WAVPACK_CAPS);
-          return;
+          ++count_wv;
+          break;
         case 0xb:              /* ID_WVC_BITSTREAM */
-          gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY,
-              WAVPACK_CORRECTION_CAPS);
-          return;
+          ++count_wvc;
+          break;
         default:
           break;
       }
+      if (count_wv >= 5 || count_wvc >= 5)
+        break;
     }
     offset += sublen;
   }
+
+  /* check for second block header */
+  data = gst_type_find_peek (tf, 8 + blocksize, 4);
+  if (data != NULL && memcmp (data, "wvpk", 4) == 0) {
+    GST_DEBUG ("found second block sync");
+    base_prob = GST_TYPE_FIND_LIKELY;
+  }
+
+  GST_DEBUG ("wvc=%d, wv=%d", count_wvc, count_wv);
+
+  if (count_wvc > 0 && count_wvc > count_wv) {
+    gst_type_find_suggest (tf,
+        MIN (base_prob + 5 * count_wvc, GST_TYPE_FIND_NEARLY_CERTAIN),
+        WAVPACK_CORRECTION_CAPS);
+  } else if (count_wv > 0) {
+    gst_type_find_suggest (tf,
+        MIN (base_prob + 5 * count_wv, GST_TYPE_FIND_NEARLY_CERTAIN),
+        WAVPACK_CAPS);
+  }
 }
 
 /*** application/postscrip ***/
@@ -2574,7 +2674,7 @@ mpeg_video_stream_type_find (GstTypeFind * tf, gpointer unused)
 
     gst_type_find_suggest_simple (tf, probability, "video/mpeg",
         "systemstream", G_TYPE_BOOLEAN, FALSE,
-        "mpegversion", G_TYPE_INT, 1, NULL);
+        "mpegversion", G_TYPE_INT, 1, "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
   }
 }
 
@@ -2796,6 +2896,12 @@ qt_type_find (GstTypeFind * tf, gpointer unused)
       break;
     }
 
+    if (STRNCMP (&data[4], "ftypisml", 8) == 0) {
+      tip = GST_TYPE_FIND_MAXIMUM;
+      variant = "iso-fragmented";
+      break;
+    }
+
     /* box/atom types that are in common with ISO base media file format */
     if (STRNCMP (&data[4], "moov", 4) == 0 ||
         STRNCMP (&data[4], "mdat", 4) == 0 ||
@@ -4236,6 +4342,66 @@ paris_type_find (GstTypeFind * tf, gpointer unused)
   }
 }
 
+/*** audio/x-sbc ***/
+static GstStaticCaps sbc_caps = GST_STATIC_CAPS ("audio/x-sbc");
+#define SBC_CAPS (gst_static_caps_get(&sbc_caps))
+
+static gsize
+sbc_check_header (const guint8 * data, gsize len, guint * rate,
+    guint * channels)
+{
+  static const guint16 sbc_rates[4] = { 16000, 32000, 44100, 48000 };
+  static const guint8 sbc_blocks[4] = { 4, 8, 12, 16 };
+  guint n_blocks, ch_mode, n_subbands, bitpool;
+
+  if (data[0] != 0x9C || len < 4)
+    return 0;
+
+  n_blocks = sbc_blocks[(data[1] >> 4) & 0x03];
+  ch_mode = (data[1] >> 2) & 0x03;
+  n_subbands = (data[1] & 0x01) ? 8 : 4;
+  bitpool = data[2];
+  if (bitpool < 2)
+    return 0;
+
+  *rate = sbc_rates[(data[1] >> 6) & 0x03];
+  *channels = (ch_mode == 0) ? 1 : 2;
+
+  if (ch_mode == 0)
+    return 4 + (n_subbands * 1) / 2 + (n_blocks * 1 * bitpool) / 8;
+  else if (ch_mode == 1)
+    return 4 + (n_subbands * 2) / 2 + (n_blocks * 2 * bitpool) / 8;
+  else if (ch_mode == 2)
+    return 4 + (n_subbands * 2) / 2 + (n_blocks * bitpool) / 8;
+  else if (ch_mode == 3)
+    return 4 + (n_subbands * 2) / 2 + (n_subbands + n_blocks * bitpool) / 8;
+
+  return 0;
+}
+
+static void
+sbc_type_find (GstTypeFind * tf, gpointer unused)
+{
+  const guint8 *data;
+  gsize frame_len;
+  guint i, rate, channels, offset = 0;
+
+  for (i = 0; i < 10; ++i) {
+    data = gst_type_find_peek (tf, offset, 8);
+    if (data == NULL)
+      return;
+
+    frame_len = sbc_check_header (data, 8, &rate, &channels);
+    if (frame_len == 0)
+      return;
+
+    offset += frame_len;
+  }
+  gst_type_find_suggest_simple (tf, GST_TYPE_FIND_POSSIBLE, "audio/x-sbc",
+      "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, channels,
+      "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
+}
+
 /*** audio/iLBC-sh ***/
 /* NOTE: do not replace this function with two TYPE_FIND_REGISTER_START_WITH */
 static GstStaticCaps ilbc_caps = GST_STATIC_CAPS ("audio/iLBC-sh");
@@ -4594,6 +4760,93 @@ dvdiso_type_find (GstTypeFind * tf, gpointer private)
       "application/octet-stream", NULL);
 }
 
+/* SSA/ASS subtitles
+ *
+ * http://en.wikipedia.org/wiki/SubStation_Alpha
+ * http://matroska.org/technical/specs/subtitles/ssa.html
+ */
+static void
+ssa_type_find (GstTypeFind * tf, gpointer private)
+{
+  const gchar *start, *end, *ver_str, *media_type = NULL;
+  const guint8 *data;
+  gchar *str, *script_type, *p = NULL;
+  gint64 len;
+
+  data = gst_type_find_peek (tf, 0, 32);
+
+  if (data == NULL)
+    return;
+
+  /* there might be a BOM at the beginning */
+  if (memcmp (data, "[Script Info]", 13) != 0 &&
+      memcmp (data + 2, "[Script Info]", 13) != 0 &&
+      memcmp (data + 3, "[Script Info]", 13) != 0 &&
+      memcmp (data + 4, "[Script Info]", 13) != 0) {
+    return;
+  }
+
+  /* now check if we have SSA or ASS */
+  len = gst_type_find_get_length (tf);
+  if (len > 8192)
+    len = 8192;
+
+  data = gst_type_find_peek (tf, 0, len);
+  if (data == NULL)
+    return;
+
+  /* skip BOM */
+  start = (gchar *) memchr (data, '[', 5);
+  g_assert (start);
+  len -= (start - (gchar *) data);
+
+  /* ignore anything non-UTF8 for now, in future we might at least allow
+   * other UTF variants that are clearly prefixed with the appropriate BOM */
+  if (!g_utf8_validate (start, len, &end) && (len - (end - start)) > 6) {
+    GST_FIXME ("non-UTF8 SSA/ASS file");
+    return;
+  }
+
+  /* something at start,  but not a UTF-8 BOM? */
+  if (data[0] != '[' && (data[0] != 0xEF || data[1] != 0xBB || data[2] != 0xBF))
+    return;
+
+  /* ignore any partial UTF-8 characters at the end */
+  len = end - start;
+
+  /* create a NUL-terminated string so it's easier to process it safely */
+  str = g_strndup (start, len - 1);
+  script_type = strstr (str, "ScriptType:");
+  if (script_type != NULL) {
+    gdouble version;
+
+    ver_str = script_type + 11;
+    while (*ver_str == ' ' || *ver_str == 'v' || *ver_str == 'V')
+      ++ver_str;
+    version = g_ascii_strtod (ver_str, &p);
+    if (version == 4.0 && p != NULL && *p == '+')
+      media_type = "application/x-ass";
+    else if (version >= 1.0 && version <= 4.0)
+      media_type = "application/x-ssa";
+  }
+
+  if (media_type == NULL) {
+    if (strstr (str, "[v4+ Styles]") || strstr (str, "[V4+ Styles]"))
+      media_type = "application/x-ass";
+    else if (strstr (str, "[v4 Styles]") || strstr (str, "[V4 Styles]"))
+      media_type = "application/x-ssa";
+  }
+
+  if (media_type != NULL) {
+    gst_type_find_suggest_simple (tf, GST_TYPE_FIND_MAXIMUM,
+        media_type, "parsed", G_TYPE_BOOLEAN, FALSE, NULL);
+  } else {
+    GST_WARNING ("could not detect SSA/ASS variant");
+  }
+
+  g_free (str);
+}
+
 /*** generic typefind for streams that have some data at a specific position***/
 typedef struct
 {
@@ -4850,6 +5103,8 @@ plugin_init (GstPlugin * plugin)
       "amr", "#!AMR-WB", 7, GST_TYPE_FIND_MAXIMUM);
   TYPE_FIND_REGISTER (plugin, "audio/iLBC-sh", GST_RANK_PRIMARY, ilbc_type_find,
       "ilbc", ILBC_CAPS, NULL, NULL);
+  TYPE_FIND_REGISTER (plugin, "audio/x-sbc", GST_RANK_MARGINAL, sbc_type_find,
+      "sbc", SBC_CAPS, NULL, NULL);
   TYPE_FIND_REGISTER_START_WITH (plugin, "audio/x-sid", GST_RANK_MARGINAL,
       "sid", "PSID", 4, GST_TYPE_FIND_MAXIMUM);
   TYPE_FIND_REGISTER_START_WITH (plugin, "image/x-xcf", GST_RANK_SECONDARY,
@@ -4968,6 +5223,9 @@ plugin_init (GstPlugin * plugin)
   TYPE_FIND_REGISTER (plugin, "application/octet-stream", GST_RANK_MARGINAL,
       dvdiso_type_find, NULL, NULL, NULL, NULL);
 
+  TYPE_FIND_REGISTER (plugin, "application/x-ssa", GST_RANK_SECONDARY,
+      ssa_type_find, "ssa,ass", NULL, NULL, NULL);
+
   return TRUE;
 }