]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/format.c
smoothstreamingenc: explict cast to avoid overflow
[ffmpeg] / libavformat / format.c
index 3a510cdac38680f6d12a4de5c7b6e78a498bc020..24b7205d440696077f210b1c26f72e9cc5276507 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+
+#include "avio_internal.h"
 #include "avformat.h"
+#include "id3v2.h"
 #include "internal.h"
-#include "libavutil/avstring.h"
 
 /**
  * @file
@@ -32,7 +36,7 @@ static AVInputFormat *first_iformat = NULL;
 /** head of registered output format linked list */
 static AVOutputFormat *first_oformat = NULL;
 
-AVInputFormat *av_iformat_next(AVInputFormat *f)
+AVInputFormat *av_iformat_next(const AVInputFormat *f)
 {
     if (f)
         return f->next;
@@ -40,7 +44,7 @@ AVInputFormat *av_iformat_next(AVInputFormat *f)
         return first_iformat;
 }
 
-AVOutputFormat *av_oformat_next(AVOutputFormat *f)
+AVOutputFormat *av_oformat_next(const AVOutputFormat *f)
 {
     if (f)
         return f->next;
@@ -52,7 +56,7 @@ void av_register_input_format(AVInputFormat *format)
 {
     AVInputFormat **p = &first_iformat;
 
-    while (*p != NULL)
+    while (*p)
         p = &(*p)->next;
 
     *p = format;
@@ -63,7 +67,7 @@ void av_register_output_format(AVOutputFormat *format)
 {
     AVOutputFormat **p = &first_oformat;
 
-    while (*p != NULL)
+    while (*p)
         p = &(*p)->next;
 
     *p = format;
@@ -97,24 +101,6 @@ int av_match_ext(const char *filename, const char *extensions)
     return 0;
 }
 
-static int match_format(const char *name, const char *names)
-{
-    const char *p;
-    int len, namelen;
-
-    if (!name || !names)
-        return 0;
-
-    namelen = strlen(name);
-    while ((p = strchr(names, ','))) {
-        len = FFMAX(p - names, namelen);
-        if (!av_strncasecmp(name, names, len))
-            return 1;
-        names = p + 1;
-    }
-    return !av_strcasecmp(name, names);
-}
-
 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
                                 const char *mime_type)
 {
@@ -177,7 +163,148 @@ AVInputFormat *av_find_input_format(const char *short_name)
 {
     AVInputFormat *fmt = NULL;
     while ((fmt = av_iformat_next(fmt)))
-        if (match_format(short_name, fmt->name))
+        if (av_match_name(short_name, fmt->name))
             return fmt;
     return NULL;
 }
+
+AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened,
+                                      int *score_max)
+{
+    AVProbeData lpd = *pd;
+    AVInputFormat *fmt1 = NULL, *fmt;
+    int score, id3 = 0;
+
+    if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
+        int id3len = ff_id3v2_tag_len(lpd.buf);
+        if (lpd.buf_size > id3len + 16) {
+            lpd.buf      += id3len;
+            lpd.buf_size -= id3len;
+        }
+        id3 = 1;
+    }
+
+    fmt = NULL;
+    while ((fmt1 = av_iformat_next(fmt1))) {
+        if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
+            continue;
+        score = 0;
+        if (fmt1->read_probe) {
+            score = fmt1->read_probe(&lpd);
+        } else if (fmt1->extensions) {
+            if (av_match_ext(lpd.filename, fmt1->extensions))
+                score = AVPROBE_SCORE_EXTENSION;
+        }
+        if (av_match_name(lpd.mime_type, fmt1->mime_type))
+            score = FFMAX(score, AVPROBE_SCORE_MIME);
+        if (score > *score_max) {
+            *score_max = score;
+            fmt        = fmt1;
+        } else if (score == *score_max)
+            fmt = NULL;
+    }
+
+    // A hack for files with huge id3v2 tags -- try to guess by file extension.
+    if (!fmt && is_opened && *score_max < AVPROBE_SCORE_EXTENSION / 2) {
+        while ((fmt = av_iformat_next(fmt)))
+            if (fmt->extensions &&
+                av_match_ext(lpd.filename, fmt->extensions)) {
+                *score_max = AVPROBE_SCORE_EXTENSION / 2;
+                break;
+            }
+    }
+
+    if (!fmt && id3 && *score_max < AVPROBE_SCORE_EXTENSION / 2 - 1) {
+        while ((fmt = av_iformat_next(fmt)))
+            if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
+                *score_max = AVPROBE_SCORE_EXTENSION / 2 - 1;
+                break;
+            }
+    }
+
+    return fmt;
+}
+
+AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
+{
+    int score = 0;
+    return av_probe_input_format2(pd, is_opened, &score);
+}
+
+/* size of probe buffer, for guessing file type from file contents */
+#define PROBE_BUF_MIN 2048
+#define PROBE_BUF_MAX (1 << 20)
+
+int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
+                          const char *filename, void *logctx,
+                          unsigned int offset, unsigned int max_probe_size)
+{
+    AVProbeData pd = { filename ? filename : "" };
+    uint8_t *buf = NULL;
+    int ret = 0, probe_size;
+    uint8_t *mime_type_opt = NULL;
+
+    if (!max_probe_size)
+        max_probe_size = PROBE_BUF_MAX;
+    else if (max_probe_size > PROBE_BUF_MAX)
+        max_probe_size = PROBE_BUF_MAX;
+    else if (max_probe_size < PROBE_BUF_MIN)
+        return AVERROR(EINVAL);
+
+    if (offset >= max_probe_size)
+        return AVERROR(EINVAL);
+    avio_skip(pb, offset);
+    max_probe_size -= offset;
+    if (pb->av_class) {
+        av_opt_get(pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type_opt);
+        pd.mime_type = (const char *)mime_type_opt;
+        mime_type_opt = NULL;
+    }
+    for (probe_size = PROBE_BUF_MIN; probe_size <= max_probe_size && !*fmt;
+         probe_size = FFMIN(probe_size << 1,
+                            FFMAX(max_probe_size, probe_size + 1))) {
+        int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX / 4 : 0;
+
+        /* Read probe data. */
+        if ((ret = av_reallocp(&buf, probe_size + AVPROBE_PADDING_SIZE)) < 0)
+            goto fail;
+        if ((ret = avio_read(pb, buf + pd.buf_size,
+                             probe_size - pd.buf_size)) < 0) {
+            /* Fail if error was not end of file, otherwise, lower score. */
+            if (ret != AVERROR_EOF)
+                goto fail;
+
+            score = 0;
+            ret   = 0;          /* error was end of file, nothing read */
+        }
+        pd.buf_size += ret;
+        pd.buf       = buf;
+
+        memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
+
+        /* Guess file format. */
+        *fmt = av_probe_input_format2(&pd, 1, &score);
+        if (*fmt) {
+            /* This can only be true in the last iteration. */
+            if (score <= AVPROBE_SCORE_MAX / 4) {
+                av_log(logctx, AV_LOG_WARNING,
+                       "Format detected only with low score of %d, "
+                       "misdetection possible!\n", score);
+            } else
+                av_log(logctx, AV_LOG_DEBUG,
+                       "Probed with size=%d and score=%d\n", probe_size, score);
+        }
+    }
+
+    if (!*fmt)
+        ret = AVERROR_INVALIDDATA;
+
+fail:
+    /* Rewind. Reuse probe buffer to avoid seeking. */
+    if (ret < 0 ||
+        (ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0) {
+        av_free(buf);
+    }
+    av_freep(&pd.mime_type);
+    return ret;
+}