| /* GStreamer |
| * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de> |
| * Copyright (C) 2005-2009 Tim-Philipp Müller <tim centricular net> |
| * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk> |
| * |
| * gsttypefindfunctions.c: collection of various typefind functions |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Library General Public |
| * License as published by the Free Software Foundation; either |
| * version 2 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Library General Public License for more details. |
| * |
| * 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., 51 Franklin St, Fifth Floor, |
| * Boston, MA 02110-1301, USA. |
| */ |
| |
| #ifdef HAVE_CONFIG_H |
| #include "config.h" |
| #endif |
| |
| #include <glib.h> |
| #include <glib/gprintf.h> |
| |
| /* don't want to add gio xdgmime typefinder if gio was disabled via configure */ |
| #ifdef HAVE_GIO |
| #include <gio/gio.h> |
| #define USE_GIO |
| #endif |
| |
| #include <gst/gst.h> |
| |
| #include <stdio.h> |
| #include <string.h> |
| #include <ctype.h> |
| |
| #include <gst/pbutils/pbutils.h> |
| #include <gst/base/gstbytereader.h> |
| |
| GST_DEBUG_CATEGORY_STATIC (type_find_debug); |
| #define GST_CAT_DEFAULT type_find_debug |
| |
| /* DataScanCtx: helper for typefind functions that scan through data |
| * step-by-step, to avoid doing a peek at each and every offset */ |
| |
| #define DATA_SCAN_CTX_CHUNK_SIZE 4096 |
| |
| typedef struct |
| { |
| guint64 offset; |
| const guint8 *data; |
| gint size; |
| } DataScanCtx; |
| |
| static inline void |
| data_scan_ctx_advance (GstTypeFind * tf, DataScanCtx * c, guint bytes_to_skip) |
| { |
| c->offset += bytes_to_skip; |
| if (G_LIKELY (c->size > bytes_to_skip)) { |
| c->size -= bytes_to_skip; |
| c->data += bytes_to_skip; |
| } else { |
| c->data += c->size; |
| c->size = 0; |
| } |
| } |
| |
| static inline gboolean |
| data_scan_ctx_ensure_data (GstTypeFind * tf, DataScanCtx * c, gint min_len) |
| { |
| const guint8 *data; |
| guint64 len; |
| guint chunk_len = MAX (DATA_SCAN_CTX_CHUNK_SIZE, min_len); |
| |
| if (G_LIKELY (c->size >= min_len)) |
| return TRUE; |
| |
| data = gst_type_find_peek (tf, c->offset, chunk_len); |
| if (G_LIKELY (data != NULL)) { |
| c->data = data; |
| c->size = chunk_len; |
| return TRUE; |
| } |
| |
| /* if there's less than our chunk size, try to get as much as we can, but |
| * always at least min_len bytes (we might be typefinding the first buffer |
| * of the stream and not have as much data available as we'd like) */ |
| len = gst_type_find_get_length (tf); |
| if (len > 0) { |
| len = CLAMP (len - c->offset, min_len, chunk_len); |
| } else { |
| len = min_len; |
| } |
| |
| data = gst_type_find_peek (tf, c->offset, len); |
| if (data != NULL) { |
| c->data = data; |
| c->size = len; |
| return TRUE; |
| } |
| |
| return FALSE; |
| } |
| |
| static inline gboolean |
| data_scan_ctx_memcmp (GstTypeFind * tf, DataScanCtx * c, guint offset, |
| const gchar * data, guint len) |
| { |
| if (!data_scan_ctx_ensure_data (tf, c, offset + len)) |
| return FALSE; |
| |
| return (memcmp (c->data + offset, data, len) == 0); |
| } |
| |
| /*** text/plain ***/ |
| static gboolean xml_check_first_element (GstTypeFind * tf, |
| const gchar * element, guint elen, gboolean strict); |
| static gboolean sdp_check_header (GstTypeFind * tf); |
| |
| static GstStaticCaps utf8_caps = GST_STATIC_CAPS ("text/plain"); |
| |
| #define UTF8_CAPS gst_static_caps_get(&utf8_caps) |
| |
| static gboolean |
| utf8_type_find_have_valid_utf8_at_offset (GstTypeFind * tf, guint64 offset, |
| GstTypeFindProbability * prob) |
| { |
| const guint8 *data; |
| |
| /* randomly decided values */ |
| guint min_size = 16; /* minimum size */ |
| guint size = 32 * 1024; /* starting size */ |
| guint probability = 95; /* starting probability */ |
| guint step = 10; /* how much we reduce probability in each |
| * iteration */ |
| |
| while (probability > step && size > min_size) { |
| data = gst_type_find_peek (tf, offset, size); |
| if (data) { |
| gchar *end; |
| gchar *start = (gchar *) data; |
| |
| if (g_utf8_validate (start, size, (const gchar **) &end) || (end - start + 4 > size)) { /* allow last char to be cut off */ |
| *prob = probability; |
| return TRUE; |
| } |
| *prob = 0; |
| return FALSE; |
| } |
| size /= 2; |
| probability -= step; |
| } |
| *prob = 0; |
| return FALSE; |
| } |
| |
| static void |
| utf8_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| GstTypeFindProbability start_prob, mid_prob; |
| guint64 length; |
| |
| /* leave xml to the xml typefinders */ |
| if (xml_check_first_element (tf, "", 0, TRUE)) |
| return; |
| |
| /* leave sdp to the sdp typefinders */ |
| if (sdp_check_header (tf)) |
| return; |
| |
| /* check beginning of stream */ |
| if (!utf8_type_find_have_valid_utf8_at_offset (tf, 0, &start_prob)) |
| return; |
| |
| GST_LOG ("start is plain text with probability of %u", start_prob); |
| |
| /* POSSIBLE is the highest probability we ever return if we can't |
| * probe into the middle of the file and don't know its length */ |
| |
| length = gst_type_find_get_length (tf); |
| if (length == 0 || length == (guint64) - 1) { |
| gst_type_find_suggest (tf, MIN (start_prob, GST_TYPE_FIND_POSSIBLE), |
| UTF8_CAPS); |
| return; |
| } |
| |
| if (length < 64 * 1024) { |
| gst_type_find_suggest (tf, start_prob, UTF8_CAPS); |
| return; |
| } |
| |
| /* check middle of stream */ |
| if (!utf8_type_find_have_valid_utf8_at_offset (tf, length / 2, &mid_prob)) |
| return; |
| |
| GST_LOG ("middle is plain text with probability of %u", mid_prob); |
| gst_type_find_suggest (tf, (start_prob + mid_prob) / 2, UTF8_CAPS); |
| } |
| |
| /*** text/utf-16 and text/utf-32} ***/ |
| /* While UTF-8 is unicode too, using text/plain for UTF-16 and UTF-32 |
| is going to break stuff. */ |
| |
| typedef struct |
| { |
| size_t bomlen; |
| const char *const bom; |
| gboolean (*checker) (const guint8 *, gint, gint); |
| int boost; |
| int endianness; |
| } GstUnicodeTester; |
| |
| static gboolean |
| check_utf16 (const guint8 * data, gint len, gint endianness) |
| { |
| GstByteReader br; |
| guint16 high, low; |
| |
| low = high = 0; |
| |
| if (len & 1) |
| return FALSE; |
| |
| gst_byte_reader_init (&br, data, len); |
| while (len >= 2) { |
| /* test first for a single 16 bit value in the BMP */ |
| if (endianness == G_BIG_ENDIAN) |
| high = gst_byte_reader_get_uint16_be_unchecked (&br); |
| else |
| high = gst_byte_reader_get_uint16_le_unchecked (&br); |
| if (high >= 0xD800 && high <= 0xDBFF) { |
| /* start of a surrogate pair */ |
| if (len < 4) |
| return FALSE; |
| len -= 2; |
| if (endianness == G_BIG_ENDIAN) |
| low = gst_byte_reader_get_uint16_be_unchecked (&br); |
| else |
| low = gst_byte_reader_get_uint16_le_unchecked (&br); |
| if (low >= 0xDC00 && low <= 0xDFFF) { |
| /* second half of the surrogate pair */ |
| } else |
| return FALSE; |
| } else { |
| if (high >= 0xDC00 && high <= 0xDFFF) |
| return FALSE; |
| } |
| len -= 2; |
| } |
| return TRUE; |
| } |
| |
| static gboolean |
| check_utf32 (const guint8 * data, gint len, gint endianness) |
| { |
| if (len & 3) |
| return FALSE; |
| while (len > 3) { |
| guint32 v; |
| if (endianness == G_BIG_ENDIAN) |
| v = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; |
| else |
| v = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; |
| if (v >= 0x10FFFF) |
| return FALSE; |
| data += 4; |
| len -= 4; |
| } |
| return TRUE; |
| } |
| |
| static void |
| unicode_type_find (GstTypeFind * tf, const GstUnicodeTester * tester, |
| guint n_tester, const char *media_type, gboolean require_bom) |
| { |
| size_t n; |
| gint len = 4; |
| const guint8 *data = gst_type_find_peek (tf, 0, len); |
| int prob = -1; |
| const gint max_scan_size = 256 * 1024; |
| int endianness = 0; |
| |
| if (!data) { |
| len = 2; |
| data = gst_type_find_peek (tf, 0, len); |
| if (!data) |
| return; |
| } |
| |
| /* find a large enough size that works */ |
| while (len < max_scan_size) { |
| size_t newlen = len << 1; |
| const guint8 *newdata = gst_type_find_peek (tf, 0, newlen); |
| if (!newdata) |
| break; |
| len = newlen; |
| data = newdata; |
| } |
| |
| for (n = 0; n < n_tester; ++n) { |
| int bom_boost = 0, tmpprob; |
| if (len >= tester[n].bomlen) { |
| if (!memcmp (data, tester[n].bom, tester[n].bomlen)) |
| bom_boost = tester[n].boost; |
| } |
| if (require_bom && bom_boost == 0) |
| continue; |
| if (!(*tester[n].checker) (data, len, tester[n].endianness)) |
| continue; |
| tmpprob = GST_TYPE_FIND_POSSIBLE - 20 + bom_boost; |
| if (tmpprob > prob) { |
| prob = tmpprob; |
| endianness = tester[n].endianness; |
| } |
| } |
| |
| if (prob > 0) { |
| GST_DEBUG ("This is valid %s %s", media_type, |
| endianness == G_BIG_ENDIAN ? "be" : "le"); |
| gst_type_find_suggest_simple (tf, prob, media_type, |
| "endianness", G_TYPE_INT, endianness, NULL); |
| } |
| } |
| |
| static GstStaticCaps utf16_caps = GST_STATIC_CAPS ("text/utf-16"); |
| |
| #define UTF16_CAPS gst_static_caps_get(&utf16_caps) |
| |
| static void |
| utf16_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| static const GstUnicodeTester utf16tester[2] = { |
| {2, "\xff\xfe", check_utf16, 10, G_LITTLE_ENDIAN}, |
| {2, "\xfe\xff", check_utf16, 20, G_BIG_ENDIAN}, |
| }; |
| unicode_type_find (tf, utf16tester, G_N_ELEMENTS (utf16tester), |
| "text/utf-16", TRUE); |
| } |
| |
| static GstStaticCaps utf32_caps = GST_STATIC_CAPS ("text/utf-32"); |
| |
| #define UTF32_CAPS gst_static_caps_get(&utf32_caps) |
| |
| static void |
| utf32_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| static const GstUnicodeTester utf32tester[2] = { |
| {4, "\xff\xfe\x00\x00", check_utf32, 10, G_LITTLE_ENDIAN}, |
| {4, "\x00\x00\xfe\xff", check_utf32, 20, G_BIG_ENDIAN} |
| }; |
| unicode_type_find (tf, utf32tester, G_N_ELEMENTS (utf32tester), |
| "text/utf-32", TRUE); |
| } |
| |
| /*** text/uri-list ***/ |
| |
| static GstStaticCaps uri_caps = GST_STATIC_CAPS ("text/uri-list"); |
| |
| #define URI_CAPS (gst_static_caps_get(&uri_caps)) |
| #define BUFFER_SIZE 16 /* If the string is < 16 bytes we're screwed */ |
| #define INC_BUFFER { \ |
| pos++; \ |
| if (pos == BUFFER_SIZE) { \ |
| pos = 0; \ |
| offset += BUFFER_SIZE; \ |
| data = gst_type_find_peek (tf, offset, BUFFER_SIZE); \ |
| if (data == NULL) return; \ |
| } else { \ |
| data++; \ |
| } \ |
| } |
| static void |
| uri_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, BUFFER_SIZE); |
| guint pos = 0; |
| guint offset = 0; |
| |
| if (data) { |
| /* Search for # comment lines */ |
| while (*data == '#') { |
| /* Goto end of line */ |
| while (*data != '\n') { |
| INC_BUFFER; |
| } |
| |
| INC_BUFFER; |
| } |
| |
| if (!g_ascii_isalpha (*data)) { |
| /* Had a non alpha char - can't be uri-list */ |
| return; |
| } |
| |
| INC_BUFFER; |
| |
| while (g_ascii_isalnum (*data)) { |
| INC_BUFFER; |
| } |
| |
| if (*data != ':') { |
| /* First non alpha char is not a : */ |
| return; |
| } |
| |
| /* Get the next 2 bytes as well */ |
| data = gst_type_find_peek (tf, offset + pos, 3); |
| if (data == NULL) |
| return; |
| |
| if (data[1] != '/' && data[2] != '/') { |
| return; |
| } |
| |
| gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, URI_CAPS); |
| } |
| } |
| |
| /*** application/itc ***/ |
| static GstStaticCaps itc_caps = GST_STATIC_CAPS ("application/itc"); |
| #define ITC_CAPS (gst_static_caps_get(&itc_caps)) |
| |
| static void |
| itc_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| guint8 magic[8] = { 0x00, 0x00, 0x01, 0x1C, 0x69, 0x74, 0x63, 0x68 }; |
| guint8 preamble[4] = { 0x00, 0x00, 0x00, 0x02 }; |
| guint8 artwork_marker[8] = { 0x00, 0x00, 0x00, 0x00, 0x61, 0x72, 0x74, 0x77 }; |
| guint8 item_marker[4] = { 0x69, 0x74, 0x65, 0x6D }; |
| GstTypeFindProbability itc_prob = GST_TYPE_FIND_NONE; |
| int i; |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8))) |
| return; |
| |
| if (memcmp (c.data, magic, 8)) |
| return; |
| |
| /* At least we found the right magic */ |
| itc_prob = GST_TYPE_FIND_MINIMUM; |
| data_scan_ctx_advance (tf, &c, 8); |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12))) |
| goto done; |
| |
| /* Check preamble 3 consecutive times */ |
| for (i = 0; i < 3; i++) { |
| if (memcmp (c.data, preamble, 4)) |
| goto done; |
| data_scan_ctx_advance (tf, &c, 4); |
| } |
| |
| itc_prob = GST_TYPE_FIND_POSSIBLE; |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8))) |
| goto done; |
| |
| if (memcmp (c.data, artwork_marker, 8)) |
| goto done; |
| |
| itc_prob = GST_TYPE_FIND_LIKELY; |
| data_scan_ctx_advance (tf, &c, 8); |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 256))) |
| goto done; |
| |
| /* ...and 256 0x00 padding bytes on what looks like the header's end */ |
| for (i = 0; i < 256; i++) { |
| if (c.data[i]) |
| goto done; |
| } |
| |
| itc_prob = GST_TYPE_FIND_NEARLY_CERTAIN; |
| data_scan_ctx_advance (tf, &c, 256); |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 8))) |
| goto done; |
| |
| if (memcmp (c.data + 4, item_marker, 4)) |
| goto done; |
| |
| itc_prob = GST_TYPE_FIND_MAXIMUM; |
| |
| done: |
| gst_type_find_suggest (tf, itc_prob, ITC_CAPS); |
| } |
| |
| /*** application/x-hls ***/ |
| |
| static GstStaticCaps hls_caps = GST_STATIC_CAPS ("application/x-hls"); |
| #define HLS_CAPS (gst_static_caps_get(&hls_caps)) |
| |
| /* See http://tools.ietf.org/html/draft-pantos-http-live-streaming-05 */ |
| static void |
| hls_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| /* Minimum useful size is #EXTM3U\n + 1 tag + ':' = 30 bytes */ |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 30))) |
| return; |
| |
| if (memcmp (c.data, "#EXTM3U", 7)) |
| return; |
| |
| data_scan_ctx_advance (tf, &c, 7); |
| |
| /* Check only the first 4KB */ |
| while (c.offset < 4096) { |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 21))) |
| return; |
| |
| /* Search for # comment lines */ |
| if (c.data[0] == '#' && (memcmp (c.data, "#EXT-X-TARGETDURATION", 21) == 0 |
| || memcmp (c.data, "#EXT-X-STREAM-INF", 17) == 0 |
| || memcmp (c.data, "#EXT-X-MEDIA", 12) == 0)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HLS_CAPS); |
| return; |
| } |
| |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| } |
| |
| |
| /*** application/xml **********************************************************/ |
| |
| #define XML_BUFFER_SIZE 16 |
| #define XML_INC_BUFFER { \ |
| pos++; \ |
| if (pos == XML_BUFFER_SIZE) { \ |
| pos = 0; \ |
| offset += XML_BUFFER_SIZE; \ |
| data = gst_type_find_peek (tf, offset, XML_BUFFER_SIZE); \ |
| if (data == NULL) return FALSE; \ |
| } else { \ |
| data++; \ |
| } \ |
| } |
| |
| #define XML_INC_BUFFER_DATA { \ |
| pos++; \ |
| if (pos >= length) { \ |
| return FALSE; \ |
| } else { \ |
| data++; \ |
| } \ |
| } |
| |
| static gboolean |
| xml_check_first_element_from_data (const guint8 * data, guint length, |
| const gchar * element, guint elen, gboolean strict) |
| { |
| gboolean got_xmldec; |
| guint pos = 0; |
| |
| g_return_val_if_fail (data != NULL, FALSE); |
| |
| if (length <= 5) |
| return FALSE; |
| |
| /* look for the XMLDec |
| * see XML spec 2.8, Prolog and Document Type Declaration |
| * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */ |
| got_xmldec = (memcmp (data, "<?xml", 5) == 0); |
| |
| if (strict && !got_xmldec) |
| return FALSE; |
| |
| /* skip XMLDec in any case if we've got one */ |
| if (got_xmldec) { |
| pos += 5; |
| data += 5; |
| } |
| |
| /* look for the first element, it has to be the requested element. Bail |
| * out if it is not within the first 4kB. */ |
| while (pos < MIN (4096, length)) { |
| while (*data != '<' && pos < MIN (4096, length)) { |
| XML_INC_BUFFER_DATA; |
| } |
| |
| XML_INC_BUFFER_DATA; |
| if (!g_ascii_isalpha (*data)) { |
| /* if not alphabetic, it's a PI or an element / attribute declaration |
| * like <?xxx or <!xxx */ |
| XML_INC_BUFFER_DATA; |
| continue; |
| } |
| |
| /* the first normal element, check if it's the one asked for */ |
| if (pos + elen + 1 >= length) |
| return FALSE; |
| return (element && strncmp ((const char *) data, element, elen) == 0); |
| } |
| |
| return FALSE; |
| } |
| |
| static gboolean |
| xml_check_first_element (GstTypeFind * tf, const gchar * element, guint elen, |
| gboolean strict) |
| { |
| gboolean got_xmldec; |
| const guint8 *data; |
| guint offset = 0; |
| guint pos = 0; |
| |
| data = gst_type_find_peek (tf, 0, XML_BUFFER_SIZE); |
| if (!data) |
| return FALSE; |
| |
| /* look for the XMLDec |
| * see XML spec 2.8, Prolog and Document Type Declaration |
| * http://www.w3.org/TR/2004/REC-xml-20040204/#sec-prolog-dtd */ |
| got_xmldec = (memcmp (data, "<?xml", 5) == 0); |
| |
| if (strict && !got_xmldec) |
| return FALSE; |
| |
| /* skip XMLDec in any case if we've got one */ |
| if (got_xmldec) { |
| pos += 5; |
| data += 5; |
| } |
| |
| /* look for the first element, it has to be the requested element. Bail |
| * out if it is not within the first 4kB. */ |
| while (data && (offset + pos) < 4096) { |
| while (*data != '<' && (offset + pos) < 4096) { |
| XML_INC_BUFFER; |
| } |
| |
| XML_INC_BUFFER; |
| if (!g_ascii_isalpha (*data)) { |
| /* if not alphabetic, it's a PI or an element / attribute declaration |
| * like <?xxx or <!xxx */ |
| XML_INC_BUFFER; |
| continue; |
| } |
| |
| /* the first normal element, check if it's the one asked for */ |
| data = gst_type_find_peek (tf, offset + pos, elen + 1); |
| return (data && element && strncmp ((char *) data, element, elen) == 0); |
| } |
| |
| return FALSE; |
| } |
| |
| static GstStaticCaps generic_xml_caps = GST_STATIC_CAPS ("application/xml"); |
| |
| #define GENERIC_XML_CAPS (gst_static_caps_get(&generic_xml_caps)) |
| static void |
| xml_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| if (xml_check_first_element (tf, "", 0, TRUE)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MINIMUM, GENERIC_XML_CAPS); |
| } |
| } |
| |
| /*** application/dash+xml ****************************************************/ |
| |
| static GstStaticCaps dash_caps = GST_STATIC_CAPS ("application/dash+xml"); |
| |
| #define DASH_CAPS gst_static_caps_get (&dash_caps) |
| |
| static void |
| dash_mpd_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| if (xml_check_first_element (tf, "MPD", 3, FALSE) || |
| xml_check_first_element (tf, "mpd", 3, FALSE)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, DASH_CAPS); |
| } |
| } |
| |
| /*** application/sdp *********************************************************/ |
| |
| static GstStaticCaps sdp_caps = GST_STATIC_CAPS ("application/sdp"); |
| |
| #define SDP_CAPS (gst_static_caps_get(&sdp_caps)) |
| static gboolean |
| sdp_check_header (GstTypeFind * tf) |
| { |
| const guint8 *data; |
| |
| data = gst_type_find_peek (tf, 0, 5); |
| if (!data) |
| return FALSE; |
| |
| /* sdp must start with v=0[\r]\n */ |
| if (memcmp (data, "v=0", 3)) |
| return FALSE; |
| |
| if (data[3] == '\r' && data[4] == '\n') |
| return TRUE; |
| if (data[3] == '\n') |
| return TRUE; |
| |
| return FALSE; |
| } |
| |
| static void |
| sdp_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| if (sdp_check_header (tf)) |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SDP_CAPS); |
| } |
| |
| /*** application/smil *********************************************************/ |
| |
| static GstStaticCaps smil_caps = GST_STATIC_CAPS ("application/smil"); |
| |
| #define SMIL_CAPS (gst_static_caps_get(&smil_caps)) |
| static void |
| smil_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| if (xml_check_first_element (tf, "smil", 4, FALSE)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SMIL_CAPS); |
| } |
| } |
| |
| /*** application/ttml+xml *****************************************************/ |
| |
| static GstStaticCaps ttml_xml_caps = GST_STATIC_CAPS ("application/ttml+xml"); |
| |
| #define TTML_XML_CAPS (gst_static_caps_get(&ttml_xml_caps)) |
| static void |
| ttml_xml_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| if (xml_check_first_element (tf, "tt", 2, FALSE)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTML_XML_CAPS); |
| } |
| } |
| |
| /*** text/html ***/ |
| |
| static GstStaticCaps html_caps = GST_STATIC_CAPS ("text/html"); |
| |
| #define HTML_CAPS gst_static_caps_get (&html_caps) |
| |
| static void |
| html_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const gchar *d, *data; |
| |
| data = (const gchar *) gst_type_find_peek (tf, 0, 16); |
| if (!data) |
| return; |
| |
| if (!g_ascii_strncasecmp (data, "<!DOCTYPE HTML", 14)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS); |
| } else if (xml_check_first_element (tf, "html", 4, FALSE)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS); |
| } else if ((d = memchr (data, '<', 16))) { |
| data = (const gchar *) gst_type_find_peek (tf, d - data, 6); |
| if (data && g_ascii_strncasecmp (data, "<html>", 6) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, HTML_CAPS); |
| } |
| } |
| } |
| |
| /*** audio/midi ***/ |
| |
| static GstStaticCaps mid_caps = GST_STATIC_CAPS ("audio/midi"); |
| |
| #define MID_CAPS gst_static_caps_get(&mid_caps) |
| static void |
| mid_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, 4); |
| |
| /* http://jedi.ks.uiuc.edu/~johns/links/music/midifile.html */ |
| if (data && data[0] == 'M' && data[1] == 'T' && data[2] == 'h' |
| && data[3] == 'd') |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MID_CAPS); |
| } |
| |
| /*** audio/mobile-xmf ***/ |
| |
| static GstStaticCaps mxmf_caps = GST_STATIC_CAPS ("audio/mobile-xmf"); |
| |
| #define MXMF_CAPS gst_static_caps_get(&mxmf_caps) |
| static void |
| mxmf_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = NULL; |
| |
| /* Search FileId "XMF_" 4 bytes */ |
| data = gst_type_find_peek (tf, 0, 4); |
| if (data && data[0] == 'X' && data[1] == 'M' && data[2] == 'F' |
| && data[3] == '_') { |
| /* Search Format version "2.00" 4 bytes */ |
| data = gst_type_find_peek (tf, 4, 4); |
| if (data && data[0] == '2' && data[1] == '.' && data[2] == '0' |
| && data[3] == '0') { |
| /* Search TypeId 2 1 byte */ |
| data = gst_type_find_peek (tf, 11, 1); |
| if (data && data[0] == 2) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MXMF_CAPS); |
| } |
| } |
| } |
| } |
| |
| |
| /*** video/x-fli ***/ |
| |
| static GstStaticCaps flx_caps = GST_STATIC_CAPS ("video/x-fli"); |
| |
| #define FLX_CAPS gst_static_caps_get(&flx_caps) |
| static void |
| flx_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, 134); |
| |
| if (data) { |
| /* check magic and the frame type of the first frame */ |
| if ((data[4] == 0x11 || data[4] == 0x12 || |
| data[4] == 0x30 || data[4] == 0x44) && |
| data[5] == 0xaf && |
| ((data[132] == 0x00 || data[132] == 0xfa) && data[133] == 0xf1)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLX_CAPS); |
| } |
| return; |
| } |
| data = gst_type_find_peek (tf, 0, 6); |
| if (data) { |
| /* check magic only */ |
| if ((data[4] == 0x11 || data[4] == 0x12 || |
| data[4] == 0x30 || data[4] == 0x44) && data[5] == 0xaf) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, FLX_CAPS); |
| } |
| return; |
| } |
| } |
| |
| /*** application/x-id3 ***/ |
| |
| static GstStaticCaps id3_caps = GST_STATIC_CAPS ("application/x-id3"); |
| |
| #define ID3_CAPS gst_static_caps_get(&id3_caps) |
| static void |
| id3v2_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, 10); |
| |
| if (data && memcmp (data, "ID3", 3) == 0 && |
| data[3] != 0xFF && data[4] != 0xFF && |
| (data[6] & 0x80) == 0 && (data[7] & 0x80) == 0 && |
| (data[8] & 0x80) == 0 && (data[9] & 0x80) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS); |
| } |
| } |
| |
| static void |
| id3v1_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, -128, 3); |
| |
| if (data && memcmp (data, "TAG", 3) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, ID3_CAPS); |
| } |
| } |
| |
| /*** application/x-ape ***/ |
| |
| static GstStaticCaps apetag_caps = GST_STATIC_CAPS ("application/x-apetag"); |
| |
| #define APETAG_CAPS gst_static_caps_get(&apetag_caps) |
| static void |
| apetag_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data; |
| |
| /* APEv1/2 at start of file */ |
| data = gst_type_find_peek (tf, 0, 8); |
| if (data && !memcmp (data, "APETAGEX", 8)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS); |
| return; |
| } |
| |
| /* APEv1/2 at end of file */ |
| data = gst_type_find_peek (tf, -32, 8); |
| if (data && !memcmp (data, "APETAGEX", 8)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, APETAG_CAPS); |
| return; |
| } |
| } |
| |
| /*** audio/x-ttafile ***/ |
| |
| static GstStaticCaps tta_caps = GST_STATIC_CAPS ("audio/x-ttafile"); |
| |
| #define TTA_CAPS gst_static_caps_get(&tta_caps) |
| static void |
| tta_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, 3); |
| |
| if (data) { |
| if (memcmp (data, "TTA", 3) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, TTA_CAPS); |
| return; |
| } |
| } |
| } |
| |
| /*** audio/x-flac ***/ |
| static GstStaticCaps flac_caps = GST_STATIC_CAPS ("audio/x-flac"); |
| |
| #define FLAC_CAPS (gst_static_caps_get(&flac_caps)) |
| |
| static void |
| flac_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4))) |
| return; |
| |
| /* standard flac (also old/broken flac-in-ogg with an initial 4-byte marker |
| * packet and without the usual packet framing) */ |
| if (memcmp (c.data, "fLaC", 4) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS); |
| return; |
| } |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6))) |
| return; |
| |
| /* flac-in-ogg, see http://flac.sourceforge.net/ogg_mapping.html */ |
| if (memcmp (c.data, "\177FLAC\001", 6) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, FLAC_CAPS); |
| return; |
| } |
| |
| /* disabled because it happily typefinds /dev/urandom as audio/x-flac, and |
| * because I yet have to see header-less flac in the wild */ |
| #if 0 |
| /* flac without headers (subset format) */ |
| /* 64K should be enough */ |
| while (c.offset < (64 * 1024)) { |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4))) |
| break; |
| |
| /* look for frame header, |
| * http://flac.sourceforge.net/format.html#frame_header |
| */ |
| if (c.data[0] == 0xff && (c.data[1] >> 2) == 0x3e) { |
| /* bit 15 in the header must be 0 */ |
| if (((c.data[1] >> 1) & 0x01) == 0x01) |
| goto advance; |
| |
| /* blocksize must be != 0x00 */ |
| if ((c.data[2] >> 4) == 0x00) |
| goto advance; |
| |
| /* samplerate must be != 0x0f */ |
| if ((c.data[2] & 0x0f) == 0x0f) |
| goto advance; |
| /* also 0 is invalid, as it means get the info from the header and we |
| * don't have headers if we are here */ |
| if ((c.data[2] & 0x0f) == 0x00) |
| goto advance; |
| |
| /* channel assignment must be < 11 */ |
| if ((c.data[3] >> 4) >= 11) |
| goto advance; |
| |
| /* sample size must be != 0x07 and != 0x05 */ |
| if (((c.data[3] >> 1) & 0x07) == 0x07) |
| goto advance; |
| if (((c.data[3] >> 1) & 0x07) == 0x05) |
| goto advance; |
| /* also 0 is invalid, as it means get the info from the header and we |
| * don't have headers if we are here */ |
| if (((c.data[3] >> 1) & 0x07) == 0x00) |
| goto advance; |
| |
| /* next bit must be 0 */ |
| if ((c.data[3] & 0x01) == 0x01) |
| goto advance; |
| |
| /* FIXME: shouldn't we include the crc check ? */ |
| |
| GST_DEBUG ("Found flac without headers at %d", (gint) c.offset); |
| gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, FLAC_CAPS); |
| return; |
| } |
| advance: |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| #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 + 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 & 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, " |
| "mpegversion = (int) { 2, 4 }, framed = (bool) false"); |
| #define AAC_CAPS (gst_static_caps_get(&aac_caps)) |
| #define AAC_AMOUNT (4096) |
| static void |
| aac_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| 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, offset, i; |
| |
| /* detect adts header or adif header. |
| * The ADIF header is 4 bytes, that should be OK. The ADTS header, on |
| * the other hand, is 14 bits only, so we require one valid frame with |
| * again a valid syncpoint on the next one (28 bits) for certainty. We |
| * require 4 kB, which is quite a lot, since frames are generally 200-400 |
| * bytes. |
| * LOAS has 2 possible syncwords, which are 11 bits and 16 bits long. |
| * The following stream syntax depends on which one is found. |
| */ |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 6))) |
| break; |
| |
| snc = GST_READ_UINT16_BE (c.data); |
| if (G_UNLIKELY ((snc & 0xfff6) == 0xfff0)) { |
| /* ADTS header - find frame length */ |
| GST_DEBUG ("Found one ADTS syncpoint at offset 0x%" G_GINT64_MODIFIER |
| "x, tracing next...", c.offset); |
| len = ((c.data[3] & 0x03) << 11) | |
| (c.data[4] << 3) | ((c.data[5] & 0xe0) >> 5); |
| |
| if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, len + 6)) { |
| GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len); |
| goto next; |
| } |
| |
| offset = len; |
| /* check if there's a second ADTS frame */ |
| snc = GST_READ_UINT16_BE (c.data + offset); |
| if ((snc & 0xfff6) == 0xfff0) { |
| GstCaps *caps; |
| guint mpegversion, sample_freq_idx, channel_config, profile_idx, rate; |
| guint8 audio_config[2]; |
| |
| mpegversion = (c.data[1] & 0x08) ? 2 : 4; |
| profile_idx = c.data[2] >> 6; |
| sample_freq_idx = ((c.data[2] & 0x3c) >> 2); |
| channel_config = ((c.data[2] & 0x01) << 2) + (c.data[3] >> 6); |
| |
| GST_DEBUG ("Found second ADTS-%d syncpoint at offset 0x%" |
| G_GINT64_MODIFIER "x, framelen %u", mpegversion, c.offset, len); |
| |
| /* 0xd and 0xe are reserved. 0xf means the sample frequency is directly |
| * specified in the header, but that's not allowed for ADTS */ |
| if (sample_freq_idx > 0xc) { |
| GST_DEBUG ("Unexpected sample frequency index %d or wrong sync", |
| sample_freq_idx); |
| goto next; |
| } |
| |
| rate = gst_codec_utils_aac_get_sample_rate_from_index (sample_freq_idx); |
| GST_LOG ("ADTS: profile=%u, rate=%u", profile_idx, rate); |
| |
| /* The ADTS frame header is slightly different from the |
| * AudioSpecificConfig defined for the MPEG-4 container, so we just |
| * construct enough of it for getting the level here. */ |
| /* ADTS counts profiles from 0 instead of 1 to save bits */ |
| audio_config[0] = (profile_idx + 1) << 3; |
| audio_config[0] |= (sample_freq_idx >> 1) & 0x7; |
| audio_config[1] = (sample_freq_idx & 0x1) << 7; |
| audio_config[1] |= (channel_config & 0xf) << 3; |
| |
| caps = gst_caps_new_simple ("audio/mpeg", |
| "framed", G_TYPE_BOOLEAN, FALSE, |
| "mpegversion", G_TYPE_INT, mpegversion, |
| "stream-format", G_TYPE_STRING, "adts", NULL); |
| |
| gst_codec_utils_aac_caps_set_level_and_profile (caps, audio_config, 2); |
| |
| /* add rate and number of channels if we can */ |
| if (channel_config != 0 && channel_config <= 7) { |
| const guint channels_map[] = { 0, 1, 2, 3, 4, 5, 6, 8 }; |
| |
| gst_caps_set_simple (caps, "channels", G_TYPE_INT, |
| channels_map[channel_config], "rate", G_TYPE_INT, rate, NULL); |
| } |
| |
| /* length of the second ADTS frame */ |
| len = ((c.data[offset + 3] & 0x03) << 11) | |
| (c.data[offset + 4] << 3) | ((c.data[offset + 5] & 0xe0) >> 5); |
| |
| if (len == 0 || !data_scan_ctx_ensure_data (tf, &c, offset + len + 6)) { |
| GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", len); |
| gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, caps); |
| } else { |
| offset += len; |
| /* find more aac sync to select correctly */ |
| /* check if there's a third/fourth/fifth/sixth ADTS frame, if there is a sixth frame, set probability to maximum:100% */ |
| for (i = 3; i <= 6; i++) { |
| len = ((c.data[offset + 3] & 0x03) << 11) | |
| (c.data[offset + 4] << 3) | ((c.data[offset + 5] & 0xe0) >> 5); |
| if (len == 0 |
| || !data_scan_ctx_ensure_data (tf, &c, offset + len + 6)) { |
| GST_DEBUG ("Wrong sync or next frame not within reach, len=%u", |
| len); |
| break; |
| } |
| snc = GST_READ_UINT16_BE (c.data + offset); |
| if ((snc & 0xfff6) == 0xfff0) { |
| GST_DEBUG ("Find %und Sync..probability is %u ", i, |
| GST_TYPE_FIND_LIKELY + 5 * (i - 2)); |
| offset += len; |
| } else { |
| break; |
| } |
| } |
| gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY + 5 * (i - 3), caps); |
| |
| } |
| gst_caps_unref (caps); |
| break; |
| } |
| |
| GST_DEBUG ("No next frame found... (should have been at 0x%x)", len); |
| } else if (G_UNLIKELY ((snc & 0xffe0) == 0x56e0 || snc == 0x4de1)) { |
| gint count; |
| |
| /* LOAS frame */ |
| GST_INFO ("Possible LOAS syncword at offset 0x%" G_GINT64_MODIFIER |
| "x, scanning for more frames...", c.offset); |
| |
| 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); |
| |
| 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); |
| best_count = count; |
| best_probability = GST_TYPE_FIND_POSSIBLE - 10 + count * 3; |
| if (best_probability >= GST_TYPE_FIND_LIKELY) |
| break; |
| } |
| } else if (!memcmp (c.data, "ADIF", 4)) { |
| /* ADIF header */ |
| gst_type_find_suggest_simple (tf, GST_TYPE_FIND_LIKELY, "audio/mpeg", |
| "framed", G_TYPE_BOOLEAN, FALSE, "mpegversion", G_TYPE_INT, 4, |
| "stream-format", G_TYPE_STRING, "adif", NULL); |
| break; |
| } |
| |
| next: |
| |
| 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 ***/ |
| |
| /* |
| * The chance that random data is identified as a valid mp3 header is 63 / 2^18 |
| * (0.024%) per try. This makes the function for calculating false positives |
| * 1 - (1 - ((63 / 2 ^18) ^ GST_MP3_TYPEFIND_MIN_HEADERS)) ^ buffersize) |
| * This has the following probabilities of false positives: |
| * datasize MIN_HEADERS |
| * (bytes) 1 2 3 4 |
| * 4096 62.6% 0.02% 0% 0% |
| * 16384 98% 0.09% 0% 0% |
| * 1 MiB 100% 5.88% 0% 0% |
| * 1 GiB 100% 100% 1.44% 0% |
| * 1 TiB 100% 100% 100% 0.35% |
| * This means that the current choice (3 headers by most of the time 4096 byte |
| * buffers is pretty safe for now. |
| * |
| * The max. size of each frame is 1440 bytes, which means that for N frames to |
| * be detected, we need 1440 * GST_MP3_TYPEFIND_MIN_HEADERS + 3 bytes of data. |
| * Assuming we step into the stream right after the frame header, this |
| * means we need 1440 * (GST_MP3_TYPEFIND_MIN_HEADERS + 1) - 1 + 3 bytes |
| * of data (5762) to always detect any mp3. |
| */ |
| |
| static const guint mp3types_bitrates[2][3][16] = |
| { {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,}, |
| {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,}, |
| {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}}, |
| {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,}, |
| {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}, |
| {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}}, |
| }; |
| |
| static const guint mp3types_freqs[3][3] = { {11025, 12000, 8000}, |
| {22050, 24000, 16000}, |
| {44100, 48000, 32000} |
| }; |
| |
| static inline guint |
| mp3_type_frame_length_from_header (guint32 header, guint * put_layer, |
| guint * put_channels, guint * put_bitrate, guint * put_samplerate, |
| gboolean * may_be_free_format, gint possible_free_framelen) |
| { |
| guint bitrate, layer, length, mode, samplerate, version, channels; |
| |
| if ((header & 0xffe00000) != 0xffe00000) |
| return 0; |
| |
| /* we don't need extension, copyright, original or |
| * emphasis for the frame length */ |
| header >>= 6; |
| |
| /* mode */ |
| mode = header & 0x3; |
| header >>= 3; |
| |
| /* padding */ |
| length = header & 0x1; |
| header >>= 1; |
| |
| /* sampling frequency */ |
| samplerate = header & 0x3; |
| if (samplerate == 3) |
| return 0; |
| header >>= 2; |
| |
| /* bitrate index */ |
| bitrate = header & 0xF; |
| if (bitrate == 0 && possible_free_framelen == -1) { |
| GST_LOG ("Possibly a free format mp3 - signaling"); |
| *may_be_free_format = TRUE; |
| } |
| if (bitrate == 15 || (bitrate == 0 && possible_free_framelen == -1)) |
| return 0; |
| |
| /* ignore error correction, too */ |
| header >>= 5; |
| |
| /* layer */ |
| layer = 4 - (header & 0x3); |
| if (layer == 4) |
| return 0; |
| header >>= 2; |
| |
| /* version 0=MPEG2.5; 2=MPEG2; 3=MPEG1 */ |
| version = header & 0x3; |
| if (version == 1) |
| return 0; |
| |
| /* lookup */ |
| channels = (mode == 3) ? 1 : 2; |
| samplerate = mp3types_freqs[version > 0 ? version - 1 : 0][samplerate]; |
| if (bitrate == 0) { |
| /* possible freeform mp3 */ |
| if (layer == 1) { |
| length *= 4; |
| length += possible_free_framelen; |
| bitrate = length * samplerate / 48000; |
| } else { |
| length += possible_free_framelen; |
| bitrate = length * samplerate / |
| ((layer == 3 && version != 3) ? 72000 : 144000); |
| } |
| /* freeform mp3 should have a higher-than-usually-allowed bitrate */ |
| GST_LOG ("calculated bitrate: %u, max usually: %u", bitrate, |
| mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][14]); |
| if (bitrate < mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][14]) |
| return 0; |
| } else { |
| /* calculating */ |
| bitrate = mp3types_bitrates[version == 3 ? 0 : 1][layer - 1][bitrate]; |
| if (layer == 1) { |
| length = ((12000 * bitrate / samplerate) + length) * 4; |
| } else { |
| length += ((layer == 3 |
| && version != 3) ? 72000 : 144000) * bitrate / samplerate; |
| } |
| } |
| |
| GST_LOG ("mp3typefind: calculated mp3 frame length of %u bytes", length); |
| GST_LOG |
| ("mp3typefind: samplerate = %u - bitrate = %u - layer = %u - version = %u" |
| " - channels = %u", samplerate, bitrate, layer, version, channels); |
| |
| if (put_layer) |
| *put_layer = layer; |
| if (put_channels) |
| *put_channels = channels; |
| if (put_bitrate) |
| *put_bitrate = bitrate; |
| if (put_samplerate) |
| *put_samplerate = samplerate; |
| |
| return length; |
| } |
| |
| |
| static GstStaticCaps mp3_caps = GST_STATIC_CAPS ("audio/mpeg, " |
| "mpegversion = (int) 1, layer = (int) [ 1, 3 ]"); |
| #define MP3_CAPS (gst_static_caps_get(&mp3_caps)) |
| /* |
| * random values for typefinding |
| * if no more data is available, we will return a probability of |
| * (found_headers/TRY_HEADERS) * (MAXIMUM * (TRY_SYNC - bytes_skipped) |
| * / TRY_SYNC) |
| * if found_headers >= MIN_HEADERS |
| */ |
| #define GST_MP3_TYPEFIND_MIN_HEADERS (2) |
| #define GST_MP3_TYPEFIND_TRY_HEADERS (5) |
| #define GST_MP3_TYPEFIND_TRY_SYNC (GST_TYPE_FIND_MAXIMUM * 100) /* 10kB */ |
| #define GST_MP3_TYPEFIND_SYNC_SIZE (2048) |
| #define GST_MP3_WRONG_HEADER (10) |
| |
| static void |
| mp3_type_find_at_offset (GstTypeFind * tf, guint64 start_off, |
| guint * found_layer, GstTypeFindProbability * found_prob) |
| { |
| const guint8 *data = NULL; |
| const guint8 *data_end = NULL; |
| guint size; |
| guint64 skipped; |
| gint last_free_offset = -1; |
| gint last_free_framelen = -1; |
| gboolean headerstart = TRUE; |
| |
| *found_layer = 0; |
| *found_prob = 0; |
| |
| size = 0; |
| skipped = 0; |
| while (skipped < GST_MP3_TYPEFIND_TRY_SYNC) { |
| if (size <= 0) { |
| size = GST_MP3_TYPEFIND_SYNC_SIZE * 2; |
| do { |
| size /= 2; |
| data = gst_type_find_peek (tf, skipped + start_off, size); |
| } while (size > 10 && !data); |
| if (!data) |
| break; |
| data_end = data + size; |
| } |
| if (*data == 0xFF) { |
| const guint8 *head_data = NULL; |
| guint layer = 0, bitrate, samplerate, channels; |
| guint found = 0; /* number of valid headers found */ |
| guint64 offset = skipped; |
| gboolean changed = FALSE; |
| |
| while (found < GST_MP3_TYPEFIND_TRY_HEADERS) { |
| guint32 head; |
| guint length; |
| guint prev_layer = 0; |
| guint prev_channels = 0, prev_samplerate = 0; |
| gboolean free = FALSE; |
| |
| if ((gint64) (offset - skipped + 4) >= 0 && |
| data + offset - skipped + 4 < data_end) { |
| head_data = data + offset - skipped; |
| } else { |
| head_data = gst_type_find_peek (tf, offset + start_off, 4); |
| } |
| if (!head_data) |
| break; |
| head = GST_READ_UINT32_BE (head_data); |
| if (!(length = mp3_type_frame_length_from_header (head, &layer, |
| &channels, &bitrate, &samplerate, &free, |
| last_free_framelen))) { |
| if (free) { |
| if (last_free_offset == -1) |
| last_free_offset = offset; |
| else { |
| last_free_framelen = offset - last_free_offset; |
| offset = last_free_offset; |
| continue; |
| } |
| } else { |
| last_free_framelen = -1; |
| } |
| |
| /* Mark the fact that we didn't find a valid header at the beginning */ |
| if (found == 0) |
| headerstart = FALSE; |
| |
| GST_LOG ("%d. header at offset %" G_GUINT64_FORMAT |
| " (0x%" G_GINT64_MODIFIER "x) was not an mp3 header " |
| "(possibly-free: %s)", found + 1, start_off + offset, |
| start_off + offset, free ? "yes" : "no"); |
| break; |
| } |
| if ((prev_layer && prev_layer != layer) || |
| /* (prev_bitrate && prev_bitrate != bitrate) || <-- VBR */ |
| (prev_samplerate && prev_samplerate != samplerate) || |
| (prev_channels && prev_channels != channels)) { |
| /* this means an invalid property, or a change, which might mean |
| * that this is not a mp3 but just a random bytestream. It could |
| * be a freaking funky encoded mp3 though. We'll just not count |
| * this header*/ |
| if (prev_layer) |
| changed = TRUE; |
| prev_layer = layer; |
| prev_channels = channels; |
| prev_samplerate = samplerate; |
| } else { |
| found++; |
| GST_LOG ("found %d. header at offset %" G_GUINT64_FORMAT " (0x%" |
| G_GINT64_MODIFIER "X)", found, start_off + offset, |
| start_off + offset); |
| } |
| offset += length; |
| } |
| g_assert (found <= GST_MP3_TYPEFIND_TRY_HEADERS); |
| if (found != 0 && head_data == NULL && |
| gst_type_find_peek (tf, offset + start_off - 1, 1) == NULL) |
| /* Incomplete last frame - don't count it. */ |
| found--; |
| if (found == GST_MP3_TYPEFIND_TRY_HEADERS || |
| (found >= GST_MP3_TYPEFIND_MIN_HEADERS && head_data == NULL)) { |
| /* we can make a valid guess */ |
| guint probability = found * GST_TYPE_FIND_MAXIMUM * |
| (GST_MP3_TYPEFIND_TRY_SYNC - skipped) / |
| GST_MP3_TYPEFIND_TRY_HEADERS / GST_MP3_TYPEFIND_TRY_SYNC; |
| |
| if (!headerstart |
| && probability > (GST_TYPE_FIND_MINIMUM + GST_MP3_WRONG_HEADER)) |
| probability -= GST_MP3_WRONG_HEADER; |
| if (probability < GST_TYPE_FIND_MINIMUM) |
| probability = GST_TYPE_FIND_MINIMUM; |
| if (start_off > 0) |
| probability /= 2; |
| if (!changed) |
| probability = (probability + GST_TYPE_FIND_MAXIMUM) / 2; |
| |
| GST_INFO |
| ("audio/mpeg calculated %u = %u * %u / %u * (%u - %" |
| G_GUINT64_FORMAT ") / %u", probability, GST_TYPE_FIND_MAXIMUM, |
| found, GST_MP3_TYPEFIND_TRY_HEADERS, GST_MP3_TYPEFIND_TRY_SYNC, |
| (guint64) skipped, GST_MP3_TYPEFIND_TRY_SYNC); |
| /* make sure we're not id3 tagged */ |
| head_data = gst_type_find_peek (tf, -128, 3); |
| if (head_data && (memcmp (head_data, "TAG", 3) == 0)) { |
| probability = 0; |
| } |
| g_assert (probability <= GST_TYPE_FIND_MAXIMUM); |
| |
| *found_prob = probability; |
| if (probability > 0) |
| *found_layer = layer; |
| return; |
| } |
| } |
| data++; |
| skipped++; |
| size--; |
| } |
| } |
| |
| static void |
| mp3_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| GstTypeFindProbability prob, mid_prob; |
| const guint8 *data; |
| guint layer, mid_layer; |
| guint64 length; |
| |
| mp3_type_find_at_offset (tf, 0, &layer, &prob); |
| length = gst_type_find_get_length (tf); |
| |
| if (length == 0 || length == (guint64) - 1) { |
| if (prob != 0) |
| goto suggest; |
| return; |
| } |
| |
| /* if we're pretty certain already, skip the additional check */ |
| if (prob >= GST_TYPE_FIND_LIKELY) |
| goto suggest; |
| |
| mp3_type_find_at_offset (tf, length / 2, &mid_layer, &mid_prob); |
| |
| if (mid_prob > 0) { |
| if (prob == 0) { |
| GST_LOG ("detected audio/mpeg only in the middle (p=%u)", mid_prob); |
| layer = mid_layer; |
| prob = mid_prob; |
| goto suggest; |
| } |
| |
| if (layer != mid_layer) { |
| GST_WARNING ("audio/mpeg layer discrepancy: %u vs. %u", layer, mid_layer); |
| return; /* FIXME: or should we just go with the one in the middle? */ |
| } |
| |
| /* detected mpeg audio both in middle of the file and at the start */ |
| prob = (prob + mid_prob) / 2; |
| goto suggest; |
| } |
| |
| /* a valid header right at the start makes it more likely |
| * that this is actually plain mpeg-1 audio */ |
| if (prob > 0) { |
| data = gst_type_find_peek (tf, 0, 4); /* use min. frame size? */ |
| if (data && mp3_type_frame_length_from_header (GST_READ_UINT32_BE (data), |
| &layer, NULL, NULL, NULL, NULL, 0) != 0) { |
| prob = MIN (prob + 10, GST_TYPE_FIND_MAXIMUM); |
| } |
| } |
| |
| if (prob > 0) |
| goto suggest; |
| |
| return; |
| |
| 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, |
| "parsed", G_TYPE_BOOLEAN, FALSE, NULL); |
| } |
| } |
| |
| /*** audio/x-musepack ***/ |
| |
| static GstStaticCaps musepack_caps = |
| GST_STATIC_CAPS ("audio/x-musepack, streamversion= (int) { 7, 8 }"); |
| |
| #define MUSEPACK_CAPS (gst_static_caps_get(&musepack_caps)) |
| static void |
| musepack_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, 4); |
| GstTypeFindProbability prop = GST_TYPE_FIND_MINIMUM; |
| gint streamversion = -1; |
| |
| if (data && memcmp (data, "MP+", 3) == 0) { |
| streamversion = 7; |
| if ((data[3] & 0x7f) == 7) { |
| prop = GST_TYPE_FIND_MAXIMUM; |
| } else { |
| prop = GST_TYPE_FIND_LIKELY + 10; |
| } |
| } else if (data && memcmp (data, "MPCK", 4) == 0) { |
| streamversion = 8; |
| prop = GST_TYPE_FIND_MAXIMUM; |
| } |
| |
| if (streamversion != -1) { |
| gst_type_find_suggest_simple (tf, prop, "audio/x-musepack", |
| "streamversion", G_TYPE_INT, streamversion, NULL); |
| } |
| } |
| |
| /*** audio/x-ac3 ***/ |
| /* FIXME 0.11: should be audio/ac3, but isn't for backwards compatibility */ |
| static GstStaticCaps ac3_caps = GST_STATIC_CAPS ("audio/x-ac3"); |
| |
| #define AC3_CAPS (gst_static_caps_get(&ac3_caps)) |
| |
| static GstStaticCaps eac3_caps = GST_STATIC_CAPS ("audio/x-eac3"); |
| |
| #define EAC3_CAPS (gst_static_caps_get(&eac3_caps)) |
| |
| struct ac3_frmsize |
| { |
| unsigned short bit_rate; |
| unsigned short frm_size[3]; |
| }; |
| |
| static const struct ac3_frmsize ac3_frmsizecod_tbl[] = { |
| {32, {64, 69, 96}}, |
| {32, {64, 70, 96}}, |
| {40, {80, 87, 120}}, |
| {40, {80, 88, 120}}, |
| {48, {96, 104, 144}}, |
| {48, {96, 105, 144}}, |
| {56, {112, 121, 168}}, |
| {56, {112, 122, 168}}, |
| {64, {128, 139, 192}}, |
| {64, {128, 140, 192}}, |
| {80, {160, 174, 240}}, |
| {80, {160, 175, 240}}, |
| {96, {192, 208, 288}}, |
| {96, {192, 209, 288}}, |
| {112, {224, 243, 336}}, |
| {112, {224, 244, 336}}, |
| {128, {256, 278, 384}}, |
| {128, {256, 279, 384}}, |
| {160, {320, 348, 480}}, |
| {160, {320, 349, 480}}, |
| {192, {384, 417, 576}}, |
| {192, {384, 418, 576}}, |
| {224, {448, 487, 672}}, |
| {224, {448, 488, 672}}, |
| {256, {512, 557, 768}}, |
| {256, {512, 558, 768}}, |
| {320, {640, 696, 960}}, |
| {320, {640, 697, 960}}, |
| {384, {768, 835, 1152}}, |
| {384, {768, 836, 1152}}, |
| {448, {896, 975, 1344}}, |
| {448, {896, 976, 1344}}, |
| {512, {1024, 1114, 1536}}, |
| {512, {1024, 1115, 1536}}, |
| {576, {1152, 1253, 1728}}, |
| {576, {1152, 1254, 1728}}, |
| {640, {1280, 1393, 1920}}, |
| {640, {1280, 1394, 1920}} |
| }; |
| |
| static void |
| ac3_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| /* Search for an ac3 frame; not necessarily right at the start, but give it |
| * a lower probability if not found right at the start. Check that the |
| * frame is followed by a second frame at the expected offset. |
| * We could also check the two ac3 CRCs, but we don't do that right now */ |
| while (c.offset < 1024) { |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 5))) |
| break; |
| |
| if (c.data[0] == 0x0b && c.data[1] == 0x77) { |
| guint bsid = c.data[5] >> 3; |
| |
| if (bsid <= 8) { |
| /* ac3 */ |
| guint fscod = c.data[4] >> 6; |
| guint frmsizecod = c.data[4] & 0x3f; |
| |
| if (fscod < 3 && frmsizecod < 38) { |
| DataScanCtx c_next = c; |
| guint frame_size; |
| |
| frame_size = ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]; |
| GST_LOG ("possible AC3 frame sync at offset %" |
| G_GUINT64_FORMAT ", size=%u", c.offset, frame_size); |
| if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) { |
| data_scan_ctx_advance (tf, &c_next, frame_size * 2); |
| |
| if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) { |
| fscod = c_next.data[4] >> 6; |
| frmsizecod = c_next.data[4] & 0x3f; |
| |
| if (fscod < 3 && frmsizecod < 38) { |
| GstTypeFindProbability prob; |
| |
| GST_LOG ("found second AC3 frame (size=%u), looks good", |
| ac3_frmsizecod_tbl[frmsizecod].frm_size[fscod]); |
| if (c.offset == 0) |
| prob = GST_TYPE_FIND_MAXIMUM; |
| else |
| prob = GST_TYPE_FIND_NEARLY_CERTAIN; |
| |
| gst_type_find_suggest (tf, prob, AC3_CAPS); |
| return; |
| } |
| } else { |
| GST_LOG ("no second AC3 frame found, false sync"); |
| } |
| } |
| } |
| } else if (bsid <= 16 && bsid > 10) { |
| /* eac3 */ |
| DataScanCtx c_next = c; |
| guint frame_size; |
| |
| frame_size = (((c.data[2] & 0x07) << 8) + c.data[3]) + 1; |
| GST_LOG ("possible E-AC3 frame sync at offset %" |
| G_GUINT64_FORMAT ", size=%u", c.offset, frame_size); |
| if (data_scan_ctx_ensure_data (tf, &c_next, (frame_size * 2) + 5)) { |
| data_scan_ctx_advance (tf, &c_next, frame_size * 2); |
| |
| if (c_next.data[0] == 0x0b && c_next.data[1] == 0x77) { |
| GstTypeFindProbability prob; |
| |
| GST_LOG ("found second E-AC3 frame, looks good"); |
| if (c.offset == 0) |
| prob = GST_TYPE_FIND_MAXIMUM; |
| else |
| prob = GST_TYPE_FIND_NEARLY_CERTAIN; |
| |
| gst_type_find_suggest (tf, prob, EAC3_CAPS); |
| return; |
| } else { |
| GST_LOG ("no second E-AC3 frame found, false sync"); |
| } |
| } |
| } else { |
| GST_LOG ("invalid AC3 BSID: %u", bsid); |
| } |
| } |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| } |
| |
| /*** audio/x-dts ***/ |
| static GstStaticCaps dts_caps = GST_STATIC_CAPS ("audio/x-dts"); |
| #define DTS_CAPS (gst_static_caps_get (&dts_caps)) |
| #define DTS_MIN_FRAMESIZE 96 |
| #define DTS_MAX_FRAMESIZE 18725 /* 16384*16/14 */ |
| |
| static gboolean |
| dts_parse_frame_header (DataScanCtx * c, guint * frame_size, |
| guint * sample_rate, guint * channels, guint * depth, guint * endianness) |
| { |
| static const int sample_rates[16] = { 0, 8000, 16000, 32000, 0, 0, 11025, |
| 22050, 44100, 0, 0, 12000, 24000, 48000, 96000, 192000 |
| }; |
| static const guint8 channels_table[16] = { 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, |
| 6, 6, 6, 7, 8, 8 |
| }; |
| guint16 hdr[8]; |
| guint32 marker; |
| guint num_blocks, chans, lfe, i; |
| |
| marker = GST_READ_UINT32_BE (c->data); |
| |
| /* raw big endian or 14-bit big endian */ |
| if (marker == 0x7FFE8001 || marker == 0x1FFFE800) { |
| *endianness = G_BIG_ENDIAN; |
| for (i = 0; i < G_N_ELEMENTS (hdr); ++i) |
| hdr[i] = GST_READ_UINT16_BE (c->data + (i * sizeof (guint16))); |
| } else |
| /* raw little endian or 14-bit little endian */ |
| if (marker == 0xFE7F0180 || marker == 0xFF1F00E8) { |
| *endianness = G_LITTLE_ENDIAN; |
| for (i = 0; i < G_N_ELEMENTS (hdr); ++i) |
| hdr[i] = GST_READ_UINT16_LE (c->data + (i * sizeof (guint16))); |
| } else { |
| return FALSE; |
| } |
| |
| GST_LOG ("dts sync marker 0x%08x at offset %u", marker, (guint) c->offset); |
| |
| /* 14-bit mode */ |
| if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) { |
| if ((hdr[2] & 0xFFF0) != 0x07F0) |
| return FALSE; |
| /* discard top 2 bits (2 void), shift in 2 */ |
| hdr[0] = (hdr[0] << 2) | ((hdr[1] >> 12) & 0x0003); |
| /* discard top 4 bits (2 void, 2 shifted into hdr[0]), shift in 4 etc. */ |
| hdr[1] = (hdr[1] << 4) | ((hdr[2] >> 10) & 0x000F); |
| hdr[2] = (hdr[2] << 6) | ((hdr[3] >> 8) & 0x003F); |
| hdr[3] = (hdr[3] << 8) | ((hdr[4] >> 6) & 0x00FF); |
| hdr[4] = (hdr[4] << 10) | ((hdr[5] >> 4) & 0x03FF); |
| hdr[5] = (hdr[5] << 12) | ((hdr[6] >> 2) & 0x0FFF); |
| hdr[6] = (hdr[6] << 14) | ((hdr[7] >> 0) & 0x3FFF); |
| g_assert (hdr[0] == 0x7FFE && hdr[1] == 0x8001); |
| *depth = 14; |
| } else { |
| *depth = 16; |
| } |
| |
| GST_LOG ("frame header: %04x%04x%04x%04x", hdr[2], hdr[3], hdr[4], hdr[5]); |
| |
| num_blocks = (hdr[2] >> 2) & 0x7F; |
| *frame_size = (((hdr[2] & 0x03) << 12) | (hdr[3] >> 4)) + 1; |
| chans = ((hdr[3] & 0x0F) << 2) | (hdr[4] >> 14); |
| *sample_rate = sample_rates[(hdr[4] >> 10) & 0x0F]; |
| lfe = (hdr[5] >> 9) & 0x03; |
| |
| if (num_blocks < 5 || *frame_size < 96 || *sample_rate == 0) |
| return FALSE; |
| |
| if (marker == 0x1FFFE800 || marker == 0xFF1F00E8) |
| *frame_size = (*frame_size * 16) / 14; /* FIXME: round up? */ |
| |
| if (chans < G_N_ELEMENTS (channels_table)) |
| *channels = channels_table[chans] + ((lfe) ? 1 : 0); |
| else |
| *channels = 0; |
| |
| return TRUE; |
| } |
| |
| static void |
| dts_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| /* Search for an dts frame; not necessarily right at the start, but give it |
| * a lower probability if not found right at the start. Check that the |
| * frame is followed by a second frame at the expected offset. */ |
| while (c.offset <= DTS_MAX_FRAMESIZE) { |
| guint frame_size = 0, rate = 0, chans = 0, depth = 0, endianness = 0; |
| |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, DTS_MIN_FRAMESIZE))) |
| return; |
| |
| if (G_UNLIKELY (dts_parse_frame_header (&c, &frame_size, &rate, &chans, |
| &depth, &endianness))) { |
| GstTypeFindProbability prob; |
| DataScanCtx next_c; |
| |
| prob = (c.offset == 0) ? GST_TYPE_FIND_LIKELY : GST_TYPE_FIND_POSSIBLE; |
| |
| /* check for second frame sync */ |
| next_c = c; |
| data_scan_ctx_advance (tf, &next_c, frame_size); |
| if (data_scan_ctx_ensure_data (tf, &next_c, 4)) { |
| GST_LOG ("frame size: %u 0x%04x", frame_size, frame_size); |
| GST_MEMDUMP ("second frame sync", next_c.data, 4); |
| if (GST_READ_UINT32_BE (c.data) == GST_READ_UINT32_BE (next_c.data)) |
| prob = GST_TYPE_FIND_MAXIMUM; |
| } |
| |
| if (chans > 0) { |
| gst_type_find_suggest_simple (tf, prob, "audio/x-dts", |
| "rate", G_TYPE_INT, rate, "channels", G_TYPE_INT, chans, |
| "depth", G_TYPE_INT, depth, "endianness", G_TYPE_INT, endianness, |
| "framed", G_TYPE_BOOLEAN, FALSE, NULL); |
| } else { |
| gst_type_find_suggest_simple (tf, prob, "audio/x-dts", |
| "rate", G_TYPE_INT, rate, "depth", G_TYPE_INT, depth, |
| "endianness", G_TYPE_INT, endianness, |
| "framed", G_TYPE_BOOLEAN, FALSE, NULL); |
| } |
| |
| return; |
| } |
| |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| } |
| |
| /*** gsm ***/ |
| |
| /* can only be detected by using the extension, in which case we use the default |
| * GSM properties */ |
| static GstStaticCaps gsm_caps = |
| GST_STATIC_CAPS ("audio/x-gsm, rate=8000, channels=1"); |
| |
| #define GSM_CAPS (gst_static_caps_get(&gsm_caps)) |
| |
| /*** wavpack ***/ |
| |
| static GstStaticCaps wavpack_caps = |
| GST_STATIC_CAPS ("audio/x-wavpack, framed = (boolean) false"); |
| |
| #define WAVPACK_CAPS (gst_static_caps_get(&wavpack_caps)) |
| |
| static GstStaticCaps wavpack_correction_caps = |
| GST_STATIC_CAPS ("audio/x-wavpack-correction, framed = (boolean) false"); |
| |
| #define WAVPACK_CORRECTION_CAPS (gst_static_caps_get(&wavpack_correction_caps)) |
| |
| 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) |
| return; |
| |
| if (data[0] != 'w' || data[1] != 'v' || data[2] != 'p' || data[3] != 'k') |
| return; |
| |
| /* Note: wavpack blocks can be fairly large (easily 60-110k), possibly |
| * larger than the max. limits imposed by certain typefinding elements |
| * like id3demux or apedemux, so typefinding is most likely only going to |
| * 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 < 8 + blocksize) { |
| guint32 sublen; |
| |
| /* get chunk header */ |
| GST_LOG ("peeking at chunk at offset 0x%04x", (guint) offset); |
| data = gst_type_find_peek (tf, offset, 4); |
| if (data == NULL) |
| break; |
| sublen = ((guint32) data[1]) << 1; |
| if (data[0] & 0x80) { |
| sublen |= (((guint32) data[2]) << 9) | (((guint32) data[3]) << 17); |
| sublen += 1 + 3; /* id + length */ |
| } else { |
| sublen += 1 + 1; /* id + length */ |
| } |
| if (offset + sublen > 8 + blocksize) { |
| GST_LOG ("chunk length too big (%u > %" G_GUINT64_FORMAT ")", sublen, |
| blocksize - offset); |
| break; |
| } |
| if ((data[0] & 0x20) == 0) { |
| switch (data[0] & 0x0f) { |
| case 0xa: /* ID_WV_BITSTREAM */ |
| case 0xc: /* ID_WVX_BITSTREAM */ |
| ++count_wv; |
| break; |
| case 0xb: /* ID_WVC_BITSTREAM */ |
| ++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 ***/ |
| static GstStaticCaps postscript_caps = |
| GST_STATIC_CAPS ("application/postscript"); |
| |
| #define POSTSCRIPT_CAPS (gst_static_caps_get(&postscript_caps)) |
| |
| static void |
| postscript_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data = gst_type_find_peek (tf, 0, 3); |
| if (!data) |
| return; |
| |
| if (data[0] == 0x04) |
| data++; |
| if (data[0] == '%' && data[1] == '!') |
| gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, POSTSCRIPT_CAPS); |
| |
| } |
| |
| /*** image/svg+xml ***/ |
| static GstStaticCaps svg_caps = GST_STATIC_CAPS ("image/svg+xml"); |
| |
| #define SVG_CAPS (gst_static_caps_get(&svg_caps)) |
| |
| static void |
| svg_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| static const gchar svg_doctype[] = "!DOCTYPE svg"; |
| static const gchar svg_tag[] = "<svg"; |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| while (c.offset <= 1024) { |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 12))) |
| break; |
| |
| if (memcmp (svg_doctype, c.data, 12) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, SVG_CAPS); |
| return; |
| } else if (memcmp (svg_tag, c.data, 4) == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, SVG_CAPS); |
| return; |
| } |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| } |
| |
| /*** multipart/x-mixed-replace mimestream ***/ |
| |
| static GstStaticCaps multipart_caps = |
| GST_STATIC_CAPS ("multipart/x-mixed-replace"); |
| #define MULTIPART_CAPS gst_static_caps_get(&multipart_caps) |
| |
| /* multipart/x-mixed replace is: |
| * <maybe some whitespace>--<some ascii chars>[\r]\n |
| * <more ascii chars>[\r]\nContent-type:<more ascii>[\r]\n */ |
| static void |
| multipart_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data; |
| const guint8 *x; |
| |
| #define MULTIPART_MAX_BOUNDARY_OFFSET 16 |
| data = gst_type_find_peek (tf, 0, MULTIPART_MAX_BOUNDARY_OFFSET); |
| if (!data) |
| return; |
| |
| for (x = data; |
| x - data < MULTIPART_MAX_BOUNDARY_OFFSET - 2 && g_ascii_isspace (*x); |
| x++); |
| if (x[0] != '-' || x[1] != '-') |
| return; |
| |
| /* Could be okay, peek what should be enough for a complete header */ |
| #define MULTIPART_MAX_HEADER_SIZE 256 |
| data = gst_type_find_peek (tf, 0, MULTIPART_MAX_HEADER_SIZE); |
| if (!data) |
| return; |
| |
| for (x = data; x - data < MULTIPART_MAX_HEADER_SIZE - 14; x++) { |
| if (!isascii (*x)) { |
| return; |
| } |
| if (*x == '\n' && |
| !g_ascii_strncasecmp ("content-type:", (gchar *) x + 1, 13)) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_MAXIMUM, MULTIPART_CAPS); |
| return; |
| } |
| } |
| } |
| |
| /*** video/mpeg systemstream ***/ |
| static GstStaticCaps mpeg_sys_caps = GST_STATIC_CAPS ("video/mpeg, " |
| "systemstream = (boolean) true, mpegversion = (int) [ 1, 2 ]"); |
| |
| #define MPEG_SYS_CAPS gst_static_caps_get(&mpeg_sys_caps) |
| #define IS_MPEG_HEADER(data) (G_UNLIKELY((((guint8 *)(data))[0] == 0x00) && \ |
| (((guint8 *)(data))[1] == 0x00) && \ |
| (((guint8 *)(data))[2] == 0x01))) |
| |
| #define IS_MPEG_PACK_CODE(b) ((b) == 0xBA) |
| #define IS_MPEG_SYS_CODE(b) ((b) == 0xBB) |
| #define IS_MPEG_PACK_HEADER(data) (IS_MPEG_HEADER (data) && \ |
| IS_MPEG_PACK_CODE (((guint8 *)(data))[3])) |
| |
| #define IS_MPEG_PES_CODE(b) (((b) & 0xF0) == 0xE0 || ((b) & 0xF0) == 0xC0 || \ |
| (b) >= 0xBD) |
| #define IS_MPEG_PES_HEADER(data) (IS_MPEG_HEADER (data) && \ |
| IS_MPEG_PES_CODE (((guint8 *)(data))[3])) |
| |
| #define MPEG2_MAX_PROBE_LENGTH (128 * 1024) /* 128kB should be 64 packs of the |
| * most common 2kB pack size. */ |
| |
| #define MPEG2_MIN_SYS_HEADERS 2 |
| #define MPEG2_MAX_SYS_HEADERS 5 |
| |
| static gboolean |
| mpeg_sys_is_valid_pack (GstTypeFind * tf, const guint8 * data, guint len, |
| guint * pack_size) |
| { |
| /* Check the pack header @ offset for validity, assuming that the 4 byte header |
| * itself has already been checked. */ |
| guint8 stuff_len; |
| |
| if (len < 12) |
| return FALSE; |
| |
| /* Check marker bits */ |
| if ((data[4] & 0xC4) == 0x44) { |
| /* MPEG-2 PACK */ |
| if (len < 14) |
| return FALSE; |
| |
| if ((data[6] & 0x04) != 0x04 || |
| (data[8] & 0x04) != 0x04 || |
| (data[9] & 0x01) != 0x01 || (data[12] & 0x03) != 0x03) |
| return FALSE; |
| |
| stuff_len = data[13] & 0x07; |
| |
| /* Check the following header bytes, if we can */ |
| if ((14 + stuff_len + 4) <= len) { |
| if (!IS_MPEG_HEADER (data + 14 + stuff_len)) |
| return FALSE; |
| } |
| if (pack_size) |
| *pack_size = 14 + stuff_len; |
| return TRUE; |
| } else if ((data[4] & 0xF1) == 0x21) { |
| /* MPEG-1 PACK */ |
| if ((data[6] & 0x01) != 0x01 || |
| (data[8] & 0x01) != 0x01 || |
| (data[9] & 0x80) != 0x80 || (data[11] & 0x01) != 0x01) |
| return FALSE; |
| |
| /* Check the following header bytes, if we can */ |
| if ((12 + 4) <= len) { |
| if (!IS_MPEG_HEADER (data + 12)) |
| return FALSE; |
| } |
| if (pack_size) |
| *pack_size = 12; |
| return TRUE; |
| } |
| |
| return FALSE; |
| } |
| |
| static gboolean |
| mpeg_sys_is_valid_pes (GstTypeFind * tf, const guint8 * data, guint len, |
| guint * pack_size) |
| { |
| guint pes_packet_len; |
| |
| /* Check the PES header at the given position, assuming the header code itself |
| * was already checked */ |
| if (len < 6) |
| return FALSE; |
| |
| /* For MPEG Program streams, unbounded PES is not allowed, so we must have a |
| * valid length present */ |
| pes_packet_len = GST_READ_UINT16_BE (data + 4); |
| if (pes_packet_len == 0) |
| return FALSE; |
| |
| /* Check the following header, if we can */ |
| if (6 + pes_packet_len + 4 <= len) { |
| if (!IS_MPEG_HEADER (data + 6 + pes_packet_len)) |
| return FALSE; |
| } |
| |
| if (pack_size) |
| *pack_size = 6 + pes_packet_len; |
| return TRUE; |
| } |
| |
| static gboolean |
| mpeg_sys_is_valid_sys (GstTypeFind * tf, const guint8 * data, guint len, |
| guint * pack_size) |
| { |
| guint sys_hdr_len; |
| |
| /* Check the System header at the given position, assuming the header code itself |
| * was already checked */ |
| if (len < 6) |
| return FALSE; |
| sys_hdr_len = GST_READ_UINT16_BE (data + 4); |
| if (sys_hdr_len < 6) |
| return FALSE; |
| |
| /* Check the following header, if we can */ |
| if (6 + sys_hdr_len + 4 <= len) { |
| if (!IS_MPEG_HEADER (data + 6 + sys_hdr_len)) |
| return FALSE; |
| } |
| |
| if (pack_size) |
| *pack_size = 6 + sys_hdr_len; |
| |
| return TRUE; |
| } |
| |
| /* calculation of possibility to identify random data as mpeg systemstream: |
| * bits that must match in header detection: 32 (or more) |
| * chance that random data is identifed: 1/2^32 |
| * chance that MPEG2_MIN_PACK_HEADERS headers are identified: |
| * 1/2^(32*MPEG2_MIN_PACK_HEADERS) |
| * chance that this happens in MPEG2_MAX_PROBE_LENGTH bytes: |
| * 1-(1+1/2^(32*MPEG2_MIN_PACK_HEADERS)^MPEG2_MAX_PROBE_LENGTH) |
| * for current values: |
| * 1-(1+1/2^(32*4)^101024) |
| * = <some_number> |
| * Since we also check marker bits and pes packet lengths, this probability is a |
| * very coarse upper bound. |
| */ |
| static void |
| mpeg_sys_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| const guint8 *data, *data0, *first_sync, *end; |
| gint mpegversion = 0; |
| guint pack_headers = 0; |
| guint pes_headers = 0; |
| guint pack_size; |
| guint since_last_sync = 0; |
| guint32 sync_word = 0xffffffff; |
| guint potential_headers = 0; |
| |
| G_STMT_START { |
| gint len; |
| |
| len = MPEG2_MAX_PROBE_LENGTH; |
| |
| while (len >= 16) { |
| data = gst_type_find_peek (tf, 0, 5 + len); |
| if (data != NULL) |
| break; |
| len = len / 2; |
| } |
| |
| if (!data) |
| return; |
| |
| end = data + len; |
| } |
| G_STMT_END; |
| |
| data0 = data; |
| first_sync = NULL; |
| |
| while (data < end) { |
| sync_word <<= 8; |
| if (sync_word == 0x00000100) { |
| /* Found potential sync word */ |
| if (first_sync == NULL) |
| first_sync = data - 3; |
| |
| if (since_last_sync > 4) { |
| /* If more than 4 bytes since the last sync word, reset our counters, |
| * as we're only interested in counting contiguous packets */ |
| pes_headers = pack_headers = 0; |
| } |
| pack_size = 0; |
| |
| potential_headers++; |
| if (IS_MPEG_PACK_CODE (data[0])) { |
| if ((data[1] & 0xC0) == 0x40) { |
| /* MPEG-2 */ |
| mpegversion = 2; |
| } else if ((data[1] & 0xF0) == 0x20) { |
| mpegversion = 1; |
| } |
| if (mpegversion != 0 && |
| mpeg_sys_is_valid_pack (tf, data - 3, end - data + 3, &pack_size)) { |
| pack_headers++; |
| } |
| } else if (IS_MPEG_PES_CODE (data[0])) { |
| /* PES stream */ |
| if (mpeg_sys_is_valid_pes (tf, data - 3, end - data + 3, &pack_size)) { |
| pes_headers++; |
| if (mpegversion == 0) |
| mpegversion = 2; |
| } |
| } else if (IS_MPEG_SYS_CODE (data[0])) { |
| if (mpeg_sys_is_valid_sys (tf, data - 3, end - data + 3, &pack_size)) { |
| pack_headers++; |
| } |
| } |
| |
| /* If we found a packet with a known size, skip the bytes in it and loop |
| * around to check the next packet. */ |
| if (pack_size != 0) { |
| data += pack_size - 3; |
| sync_word = 0xffffffff; |
| since_last_sync = 0; |
| continue; |
| } |
| } |
| |
| sync_word |= data[0]; |
| since_last_sync++; |
| data++; |
| |
| /* If we have found MAX headers, and *some* were pes headers (pack headers |
| * are optional in an mpeg system stream) then return our high-probability |
| * result */ |
| if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MAX_SYS_HEADERS) |
| goto suggest; |
| } |
| |
| /* If we at least saw MIN headers, and *some* were pes headers (pack headers |
| * are optional in an mpeg system stream) then return a lower-probability |
| * result */ |
| if (pes_headers > 0 && (pack_headers + pes_headers) > MPEG2_MIN_SYS_HEADERS) |
| goto suggest; |
| |
| return; |
| suggest: |
| { |
| guint prob; |
| |
| prob = GST_TYPE_FIND_POSSIBLE + (10 * (pack_headers + pes_headers)); |
| prob = MIN (prob, GST_TYPE_FIND_MAXIMUM); |
| |
| /* With the above test, we get into problems when we try to typefind |
| a MPEG stream from a small amount of data, which can happen when |
| we get data pushed from a HTTP source. We thus make a second test |
| to give higher probability if all the potential headers were either |
| pack or pes headers (ie, no potential header was unrecognized). */ |
| if (potential_headers == pack_headers + pes_headers) { |
| GST_LOG ("Only %u headers, but all were recognized", potential_headers); |
| prob += 10; |
| prob = MIN (prob, GST_TYPE_FIND_MAXIMUM); |
| } |
| |
| /* lower probability if the first packet wasn't right at the start */ |
| if (data0 != first_sync && prob >= 10) |
| prob -= 10; |
| |
| GST_LOG ("Suggesting MPEG %d system stream, %d packs, %d pes, prob %u%%", |
| mpegversion, pack_headers, pes_headers, prob); |
| |
| gst_type_find_suggest_simple (tf, prob, "video/mpeg", |
| "systemstream", G_TYPE_BOOLEAN, TRUE, |
| "mpegversion", G_TYPE_INT, mpegversion, NULL); |
| } |
| }; |
| |
| /*** video/mpegts Transport Stream ***/ |
| static GstStaticCaps mpegts_caps = GST_STATIC_CAPS ("video/mpegts, " |
| "systemstream = (boolean) true, packetsize = (int) [ 188, 208 ]"); |
| #define MPEGTS_CAPS gst_static_caps_get(&mpegts_caps) |
| |
| #define GST_MPEGTS_TYPEFIND_MIN_HEADERS 4 |
| #define GST_MPEGTS_TYPEFIND_MAX_HEADERS 10 |
| #define GST_MPEGTS_MAX_PACKET_SIZE 208 |
| #define GST_MPEGTS_TYPEFIND_SYNC_SIZE \ |
| (GST_MPEGTS_TYPEFIND_MIN_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE) |
| #define GST_MPEGTS_TYPEFIND_MAX_SYNC \ |
| (GST_MPEGTS_TYPEFIND_MAX_HEADERS * GST_MPEGTS_MAX_PACKET_SIZE) |
| #define GST_MPEGTS_TYPEFIND_SCAN_LENGTH \ |
| (GST_MPEGTS_TYPEFIND_MAX_SYNC * 4) |
| |
| #define MPEGTS_HDR_SIZE 4 |
| /* Check for sync byte, error_indicator == 0 and packet has payload. |
| * Adaptation control field (data[3] & 0x30) may be zero for TS packets with |
| * null PIDs. Still, these streams are valid TS streams (for null packets, |
| * AFC is supposed to be 0x1, but the spec also says decoders should just |
| * discard any packets with AFC = 0x00) */ |
| #define IS_MPEGTS_HEADER(data) (data[0] == 0x47 && \ |
| (data[1] & 0x80) == 0x00 && \ |
| ((data[3] & 0x30) != 0x00 || \ |
| ((data[3] & 0x30) == 0x00 && (data[1] & 0x1f) == 0x1f && (data[2] & 0xff) == 0xff))) |
| |
| /* Helper function to search ahead at intervals of packet_size for mpegts |
| * headers */ |
| static gint |
| mpeg_ts_probe_headers (GstTypeFind * tf, guint64 offset, gint packet_size) |
| { |
| /* We always enter this function having found at least one header already */ |
| gint found = 1; |
| const guint8 *data = NULL; |
| |
| GST_LOG ("looking for mpeg-ts packets of size %u", packet_size); |
| while (found < GST_MPEGTS_TYPEFIND_MAX_HEADERS) { |
| offset += packet_size; |
| |
| data = gst_type_find_peek (tf, offset, MPEGTS_HDR_SIZE); |
| if (data == NULL || !IS_MPEGTS_HEADER (data)) |
| return found; |
| |
| found++; |
| GST_LOG ("mpeg-ts sync #%2d at offset %" G_GUINT64_FORMAT, found, offset); |
| } |
| |
| return found; |
| } |
| |
| /* Try and detect at least 4 packets in at most 10 packets worth of |
| * data. Need to try several possible packet sizes */ |
| static void |
| mpeg_ts_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| /* TS packet sizes to test: normal, DVHS packet size and |
| * FEC with 16 or 20 byte codes packet size. */ |
| const gint pack_sizes[] = { 188, 192, 204, 208 }; |
| const guint8 *data = NULL; |
| guint size = 0; |
| guint64 skipped = 0; |
| |
| while (skipped < GST_MPEGTS_TYPEFIND_SCAN_LENGTH) { |
| if (size < MPEGTS_HDR_SIZE) { |
| data = gst_type_find_peek (tf, skipped, GST_MPEGTS_TYPEFIND_SYNC_SIZE); |
| if (!data) |
| break; |
| size = GST_MPEGTS_TYPEFIND_SYNC_SIZE; |
| } |
| |
| /* Have at least MPEGTS_HDR_SIZE bytes at this point */ |
| if (IS_MPEGTS_HEADER (data)) { |
| gint p; |
| |
| GST_LOG ("possible mpeg-ts sync at offset %" G_GUINT64_FORMAT, skipped); |
| |
| for (p = 0; p < G_N_ELEMENTS (pack_sizes); p++) { |
| gint found; |
| |
| /* Probe ahead at size pack_sizes[p] */ |
| found = mpeg_ts_probe_headers (tf, skipped, pack_sizes[p]); |
| if (found >= GST_MPEGTS_TYPEFIND_MIN_HEADERS) { |
| gint probability; |
| |
| /* found at least 4 headers. 10 headers = MAXIMUM probability. |
| * Arbitrarily, I assigned 10% probability for each header we |
| * found, 40% -> 100% */ |
| probability = MIN (10 * found, GST_TYPE_FIND_MAXIMUM); |
| |
| gst_type_find_suggest_simple (tf, probability, "video/mpegts", |
| "systemstream", G_TYPE_BOOLEAN, TRUE, |
| "packetsize", G_TYPE_INT, pack_sizes[p], NULL); |
| return; |
| } |
| } |
| } |
| data++; |
| skipped++; |
| size--; |
| } |
| } |
| |
| #define GST_MPEGVID_TYPEFIND_TRY_PICTURES 6 |
| #define GST_MPEGVID_TYPEFIND_TRY_SYNC (100 * 1024) /* 100 kB */ |
| |
| /* Scan ahead a maximum of max_extra_offset bytes until the next IS_MPEG_HEADER |
| * offset. After the call, offset will be after the 0x000001, i.e. at the 4th |
| * byte of the MPEG header. Returns TRUE if a header was found, FALSE if not. |
| */ |
| static gboolean |
| mpeg_find_next_header (GstTypeFind * tf, DataScanCtx * c, |
| guint64 max_extra_offset) |
| { |
| guint64 extra_offset; |
| |
| for (extra_offset = 0; extra_offset <= max_extra_offset; ++extra_offset) { |
| if (!data_scan_ctx_ensure_data (tf, c, 4)) |
| return FALSE; |
| if (IS_MPEG_HEADER (c->data)) { |
| data_scan_ctx_advance (tf, c, 3); |
| return TRUE; |
| } |
| data_scan_ctx_advance (tf, c, 1); |
| } |
| return FALSE; |
| } |
| |
| /*** video/mpeg MPEG-4 elementary video stream ***/ |
| |
| static GstStaticCaps mpeg4_video_caps = GST_STATIC_CAPS ("video/mpeg, " |
| "systemstream=(boolean)false, mpegversion=4, parsed=(boolean)false"); |
| #define MPEG4_VIDEO_CAPS gst_static_caps_get(&mpeg4_video_caps) |
| |
| /* |
| * This typefind is based on the elementary video header defined in |
| * http://xhelmboyx.tripod.com/formats/mpeg-layout.txt |
| * In addition, it allows the visual object sequence header to be |
| * absent, and even the VOS header to be absent. In the latter case, |
| * a number of VOPs have to be present. |
| */ |
| static void |
| mpeg4_video_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| gboolean seen_vios_at_0 = FALSE; |
| gboolean seen_vios = FALSE; |
| gboolean seen_vos = FALSE; |
| gboolean seen_vol = FALSE; |
| guint num_vop_headers = 0; |
| guint8 sc; |
| |
| while (c.offset < GST_MPEGVID_TYPEFIND_TRY_SYNC) { |
| if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES) |
| break; |
| |
| if (!mpeg_find_next_header (tf, &c, |
| GST_MPEGVID_TYPEFIND_TRY_SYNC - c.offset)) |
| break; |
| |
| sc = c.data[0]; |
| |
| /* visual_object_sequence_start_code */ |
| if (sc == 0xB0) { |
| if (seen_vios) |
| break; /* Terminate at second vios */ |
| if (c.offset == 0) |
| seen_vios_at_0 = TRUE; |
| seen_vios = TRUE; |
| data_scan_ctx_advance (tf, &c, 2); |
| if (!mpeg_find_next_header (tf, &c, 0)) |
| break; |
| |
| sc = c.data[0]; |
| |
| /* Optional metadata */ |
| if (sc == 0xB2) |
| if (!mpeg_find_next_header (tf, &c, 24)) |
| break; |
| } |
| |
| /* visual_object_start_code (consider it optional) */ |
| if (sc == 0xB5) { |
| data_scan_ctx_advance (tf, &c, 2); |
| /* may contain ID marker and YUV clamping */ |
| if (!mpeg_find_next_header (tf, &c, 7)) |
| break; |
| |
| sc = c.data[0]; |
| } |
| |
| /* video_object_start_code */ |
| if (sc <= 0x1F) { |
| if (seen_vos) |
| break; /* Terminate at second vos */ |
| seen_vos = TRUE; |
| data_scan_ctx_advance (tf, &c, 2); |
| continue; |
| } |
| |
| /* video_object_layer_start_code */ |
| if (sc >= 0x20 && sc <= 0x2F) { |
| seen_vol = TRUE; |
| data_scan_ctx_advance (tf, &c, 5); |
| continue; |
| } |
| |
| /* video_object_plane_start_code */ |
| if (sc == 0xB6) { |
| num_vop_headers++; |
| data_scan_ctx_advance (tf, &c, 2); |
| continue; |
| } |
| |
| /* Unknown start code. */ |
| } |
| |
| if (num_vop_headers > 0 || seen_vol) { |
| GstTypeFindProbability probability = 0; |
| |
| GST_LOG ("Found %d pictures, vios: %d, vos:%d, vol:%d", num_vop_headers, |
| seen_vios, seen_vos, seen_vol); |
| |
| if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios_at_0 |
| && seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_MAXIMUM - 1; |
| else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vios |
| && seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_NEARLY_CERTAIN - 1; |
| else if (seen_vios_at_0 && seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6; |
| else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vos |
| && seen_vol) |
| probability = GST_TYPE_FIND_NEARLY_CERTAIN - 6; |
| else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES && seen_vol) |
| probability = GST_TYPE_FIND_NEARLY_CERTAIN - 9; |
| else if (num_vop_headers >= GST_MPEGVID_TYPEFIND_TRY_PICTURES) |
| probability = GST_TYPE_FIND_LIKELY - 1; |
| else if (num_vop_headers > 2 && seen_vios && seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_LIKELY - 9; |
| else if (seen_vios && seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_LIKELY - 20; |
| else if (num_vop_headers > 0 && seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_POSSIBLE; |
| else if (num_vop_headers > 0) |
| probability = GST_TYPE_FIND_POSSIBLE - 10; |
| else if (seen_vos && seen_vol) |
| probability = GST_TYPE_FIND_POSSIBLE - 20; |
| |
| gst_type_find_suggest (tf, probability, MPEG4_VIDEO_CAPS); |
| } |
| } |
| |
| /*** video/x-h263 H263 video stream ***/ |
| static GstStaticCaps h263_video_caps = |
| GST_STATIC_CAPS ("video/x-h263, variant=(string)itu"); |
| |
| #define H263_VIDEO_CAPS gst_static_caps_get(&h263_video_caps) |
| |
| #define H263_MAX_PROBE_LENGTH (128 * 1024) |
| |
| static void |
| h263_video_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| guint64 data = 0xffff; /* prevents false positive for first 2 bytes */ |
| guint64 psc = 0; |
| guint8 ptype = 0; |
| guint format; |
| guint good = 0; |
| guint bad = 0; |
| guint pc_type, pb_mode; |
| |
| while (c.offset < H263_MAX_PROBE_LENGTH) { |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4))) |
| break; |
| |
| /* Find the picture start code */ |
| data = (data << 8) + c.data[0]; |
| psc = data & G_GUINT64_CONSTANT (0xfffffc0000); |
| if (psc == 0x800000) { |
| /* Found PSC */ |
| /* PTYPE */ |
| ptype = (data & 0x3fc) >> 2; |
| /* Source Format */ |
| format = ptype & 0x07; |
| |
| /* Now that we have a Valid PSC, check if we also have a valid PTYPE and |
| the Source Format, which should range between 1 and 5 */ |
| if (((ptype >> 6) == 0x2) && (format > 0 && format < 6)) { |
| pc_type = data & 0x02; |
| pb_mode = c.data[1] & 0x20 >> 4; |
| if (!pc_type && pb_mode) |
| bad++; |
| else |
| good++; |
| } else |
| bad++; |
| |
| /* FIXME: maybe bail out early if we get mostly bad syncs ? */ |
| } |
| |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| |
| GST_LOG ("good: %d, bad: %d", good, bad); |
| |
| if (good > 2 * bad) |
| gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H263_VIDEO_CAPS); |
| |
| return; |
| } |
| |
| /*** video/x-h264 H264 elementary video stream ***/ |
| |
| static GstStaticCaps h264_video_caps = |
| GST_STATIC_CAPS ("video/x-h264,stream-format=byte-stream"); |
| |
| #define H264_VIDEO_CAPS gst_static_caps_get(&h264_video_caps) |
| |
| #define H264_MAX_PROBE_LENGTH (128 * 1024) /* 128kB for HD should be enough. */ |
| |
| static void |
| h264_video_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| /* Stream consists of: a series of sync codes (00 00 00 01) followed |
| * by NALs |
| */ |
| gboolean seen_idr = FALSE; |
| gboolean seen_sps = FALSE; |
| gboolean seen_pps = FALSE; |
| gboolean seen_ssps = FALSE; |
| int nut, ref; |
| int good = 0; |
| int bad = 0; |
| |
| while (c.offset < H264_MAX_PROBE_LENGTH) { |
| if (G_UNLIKELY (!data_scan_ctx_ensure_data (tf, &c, 4))) |
| break; |
| |
| if (IS_MPEG_HEADER (c.data)) { |
| nut = c.data[3] & 0x9f; /* forbiden_zero_bit | nal_unit_type */ |
| ref = c.data[3] & 0x60; /* nal_ref_idc */ |
| |
| /* if forbidden bit is different to 0 won't be h264 */ |
| if (nut > 0x1f) { |
| bad++; |
| break; |
| } |
| |
| /* collect statistics about the NAL types */ |
| if ((nut >= 1 && nut <= 13) || nut == 19) { |
| if ((nut == 5 && ref == 0) || |
| ((nut == 6 || (nut >= 9 && nut <= 12)) && ref != 0)) { |
| bad++; |
| } else { |
| if (nut == 7) |
| seen_sps = TRUE; |
| else if (nut == 8) |
| seen_pps = TRUE; |
| else if (nut == 5) |
| seen_idr = TRUE; |
| |
| good++; |
| } |
| } else if (nut >= 14 && nut <= 33) { |
| if (nut == 15) { |
| seen_ssps = TRUE; |
| good++; |
| } else if (nut == 14 || nut == 20) { |
| /* Sometimes we see NAL 14 or 20 without SSPS |
| * if dropped into the middle of a stream - |
| * just ignore those (don't add to bad count) */ |
| if (seen_ssps) |
| good++; |
| } else { |
| /* reserved */ |
| /* Theoretically these are good, since if they exist in the |
| stream it merely means that a newer backwards-compatible |
| h.264 stream. But we should be identifying that separately. */ |
| bad++; |
| } |
| } else { |
| /* unspecified, application specific */ |
| /* don't consider these bad */ |
| } |
| |
| GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, idr:%d ssps:%d", good, bad, |
| seen_pps, seen_sps, seen_idr, seen_ssps); |
| |
| if (seen_sps && seen_pps && seen_idr && good >= 10 && bad < 4) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_LIKELY, H264_VIDEO_CAPS); |
| return; |
| } |
| |
| data_scan_ctx_advance (tf, &c, 4); |
| } |
| data_scan_ctx_advance (tf, &c, 1); |
| } |
| |
| GST_LOG ("good:%d, bad:%d, pps:%d, sps:%d, idr:%d ssps=%d", good, bad, |
| seen_pps, seen_sps, seen_idr, seen_ssps); |
| |
| if (good >= 2 && bad == 0) { |
| gst_type_find_suggest (tf, GST_TYPE_FIND_POSSIBLE, H264_VIDEO_CAPS); |
| } |
| } |
| |
| /*** video/x-h265 H265 elementary video stream ***/ |
| |
| static GstStaticCaps h265_video_caps = |
| GST_STATIC_CAPS ("video/x-h265,stream-format=byte-stream"); |
| |
| #define H265_VIDEO_CAPS gst_static_caps_get(&h265_video_caps) |
| |
| #define H265_MAX_PROBE_LENGTH (128 * 1024) /* 128kB for HD should be enough. */ |
| |
| static void |
| h265_video_type_find (GstTypeFind * tf, gpointer unused) |
| { |
| DataScanCtx c = { 0, NULL, 0 }; |
| |
| /* Stream consists of: a series of sync codes (00 00 00 01) followed |
| * by NALs |
| */ |
| gboolean seen_irap = FALSE; |
| gboolean seen_vps = FALSE; |
|