[FFmpeg-cvslog] r22296 - in trunk/libavformat: internal.h utils.c

stefano subversion
Sun Mar 7 23:42:11 CET 2010


Author: stefano
Date: Sun Mar  7 23:42:11 2010
New Revision: 22296

Log:
Move the probe loop from av_open_input_file() into its own method
av_probe_input_buffer() so that it can be reused. Here are a few
differences to the original way things were probed:

- maximum probe buffer size can be specified as a parameter.

- offset within the stream to probe from can be specified as a parameter.

- instead of seeking back to the start each time a probe fails, stream
  data is appended to the reallocated buffer. This lowers the amount
  of data read from the stream (there is no repetition) and results in
  fewer closed and reopened streams (when seeking fails).

Patch by Micah F. Galizia printf("%s%s@%s.%s", "micah", "galizia", "gmail", "com").

Modified:
   trunk/libavformat/internal.h
   trunk/libavformat/utils.c

Modified: trunk/libavformat/internal.h
==============================================================================
--- trunk/libavformat/internal.h	Sun Mar  7 23:18:37 2010	(r22295)
+++ trunk/libavformat/internal.h	Sun Mar  7 23:42:11 2010	(r22296)
@@ -37,4 +37,24 @@ void ff_interleave_add_packet(AVFormatCo
 
 void av_read_frame_flush(AVFormatContext *s);
 
+/**
+ * Probes a bytestream to determine the input format. Each time a
+ * probe returns with a score that is too low, the probe buffer size
+ * is increased and another attempt is made. When the maximum probe
+ * size is reached, the input format with the highest score is
+ * returned.
+ *
+ * @param pb the bytestream to probe, it may be closed and opened again
+ * @param fmt the input format is put here
+ * @param filename the filename of the stream
+ * @param logctx the log context
+ * @param offset the offset within the bytestream to probe from
+ * @param max_probe_size the maximum probe buffer size (zero for default)
+ * @return 0 if OK, a negative value corresponding to an AVERROR(xxx)
+ * code otherwise
+ */
+int ff_probe_input_buffer(ByteIOContext **pb, AVInputFormat **fmt,
+                          const char *filename, void *logctx,
+                          unsigned int offset, unsigned int max_probe_size);
+
 #endif /* AVFORMAT_INTERNAL_H */

Modified: trunk/libavformat/utils.c
==============================================================================
--- trunk/libavformat/utils.c	Sun Mar  7 23:18:37 2010	(r22295)
+++ trunk/libavformat/utils.c	Sun Mar  7 23:42:11 2010	(r22296)
@@ -465,12 +465,71 @@ int av_open_input_stream(AVFormatContext
 #define PROBE_BUF_MIN 2048
 #define PROBE_BUF_MAX (1<<20)
 
+int ff_probe_input_buffer(ByteIOContext **pb, AVInputFormat **fmt,
+                          const char *filename, void *logctx,
+                          unsigned int offset, unsigned int max_probe_size)
+{
+    AVProbeData pd = { filename ? filename : "", NULL, -offset };
+    unsigned char *buf = NULL;
+    int probe_size;
+
+    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);
+    }
+
+    for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt; probe_size<<=1){
+        int ret, score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
+        int buf_offset = (probe_size == PROBE_BUF_MIN) ? 0 : probe_size>>1;
+
+        if (probe_size < offset) {
+            continue;
+        }
+
+        /* read probe data */
+        buf = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
+        if ((ret = get_buffer(*pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
+            av_free(buf);
+            return ret;
+        }
+        pd.buf_size += ret;
+        pd.buf = &buf[offset];
+
+        memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
+
+        /* guess file format */
+        *fmt = av_probe_input_format2(&pd, 1, &score);
+        if(*fmt){
+            if(score <= AVPROBE_SCORE_MAX/4){ //this can only be true in the last iteration
+                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);
+        }
+    }
+
+    av_free(buf);
+    if (url_fseek(*pb, 0, SEEK_SET) < 0) {
+        url_fclose(*pb);
+        if (url_fopen(pb, filename, URL_RDONLY) < 0)
+            return AVERROR(EIO);
+    }
+
+    return 0;
+}
+
 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
                        AVInputFormat *fmt,
                        int buf_size,
                        AVFormatParameters *ap)
 {
-    int err, probe_size;
+    int err;
     AVProbeData probe_data, *pd = &probe_data;
     ByteIOContext *pb = NULL;
     void *logctx= ap && ap->prealloced_context ? *ic_ptr : NULL;
@@ -496,37 +555,9 @@ int av_open_input_file(AVFormatContext *
         if (buf_size > 0) {
             url_setbufsize(pb, buf_size);
         }
-
-        for(probe_size= PROBE_BUF_MIN; probe_size<=PROBE_BUF_MAX && !fmt; probe_size<<=1){
-            int score= probe_size < PROBE_BUF_MAX ? AVPROBE_SCORE_MAX/4 : 0;
-            /* read probe data */
-            pd->buf= av_realloc(pd->buf, probe_size + AVPROBE_PADDING_SIZE);
-            pd->buf_size = get_buffer(pb, pd->buf, probe_size);
-
-            if ((int)pd->buf_size < 0) {
-                err = pd->buf_size;
-                goto fail;
-            }
-
-            memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
-            if (url_fseek(pb, 0, SEEK_SET) < 0) {
-                url_fclose(pb);
-                if (url_fopen(&pb, filename, URL_RDONLY) < 0) {
-                    pb = NULL;
-                    err = AVERROR(EIO);
-                    goto fail;
-                }
-            }
-            /* guess file format */
-            fmt = av_probe_input_format2(pd, 1, &score);
-            if(fmt){
-                if(score <= AVPROBE_SCORE_MAX/4){ //this can only be true in the last iteration
-                    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 ((err = ff_probe_input_buffer(&pb, &fmt, filename, logctx, 0, 0)) < 0) {
+            goto fail;
         }
-        av_freep(&pd->buf);
     }
 
     /* if still no format found, error */



More information about the ffmpeg-cvslog mailing list