[FFmpeg-devel] [PATCH] Introduce avio_dump_contents() and use it in lavd/lavfi.c

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Jul 17 22:37:03 CEST 2014


This is a replacement for previously proposed API
av_bprint_fd_contents().
Side-effect: lavfi input device now accepts any URL as "graph_file"
option value.
---
 libavdevice/lavfi.c   | 30 +++++++++++++++---------------
 libavformat/avio.h    |  8 ++++++++
 libavformat/aviobuf.c | 16 ++++++++++++++++
 3 files changed, 39 insertions(+), 15 deletions(-)

diff --git a/libavdevice/lavfi.c b/libavdevice/lavfi.c
index d1904dd..0388a86 100644
--- a/libavdevice/lavfi.c
+++ b/libavdevice/lavfi.c
@@ -115,23 +115,23 @@ av_cold static int lavfi_read_header(AVFormatContext *avctx)
     }
 
     if (lavfi->graph_filename) {
-        uint8_t *file_buf, *graph_buf;
-        size_t file_bufsize;
-        ret = av_file_map(lavfi->graph_filename,
-                          &file_buf, &file_bufsize, 0, avctx);
+        AVBPrint graph_file_pb;
+        AVIOContext *avio = NULL;
+        ret = avio_open(&avio, lavfi->graph_filename, AVIO_FLAG_READ);
         if (ret < 0)
-            goto end;
-
-        /* create a 0-terminated string based on the read file */
-        graph_buf = av_malloc(file_bufsize + 1);
-        if (!graph_buf) {
-            av_file_unmap(file_buf, file_bufsize);
-            FAIL(AVERROR(ENOMEM));
+            FAIL(ret);
+        av_bprint_init(&graph_file_pb, 0, AV_BPRINT_SIZE_UNLIMITED);
+        ret = avio_dump_contents(avio, &graph_file_pb);
+        avio_close(avio);
+        av_bprint_chars(&graph_file_pb, '\0', 1);
+        if (!ret && !av_bprint_is_complete(&graph_file_pb))
+            ret = AVERROR(ENOMEM);
+        if (ret) {
+            av_bprint_finalize(&graph_file_pb, NULL);
+            FAIL(ret);
         }
-        memcpy(graph_buf, file_buf, file_bufsize);
-        graph_buf[file_bufsize] = 0;
-        av_file_unmap(file_buf, file_bufsize);
-        lavfi->graph_str = graph_buf;
+        if ((ret = av_bprint_finalize(&graph_file_pb, &lavfi->graph_str)))
+            FAIL(ret);
     }
 
     if (!lavfi->graph_str)
diff --git a/libavformat/avio.h b/libavformat/avio.h
index 4004b6f..7f608fa 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -31,6 +31,7 @@
 #include "libavutil/common.h"
 #include "libavutil/dict.h"
 #include "libavutil/log.h"
+#include "libavutil/bprint.h"
 
 #include "libavformat/version.h"
 
@@ -500,4 +501,11 @@ int     avio_pause(AVIOContext *h, int pause);
 int64_t avio_seek_time(AVIOContext *h, int stream_index,
                        int64_t timestamp, int flags);
 
+/**
+ * Read contents of h into print buffer up to EOF.
+ *
+ * @return 0 for success, error code otherwise
+ */
+int avio_dump_contents(AVIOContext *h, AVBPrint *pb);
+
 #endif /* AVFORMAT_AVIO_H */
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 738459e..ba84873 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -953,6 +953,22 @@ int64_t avio_seek_time(AVIOContext *s, int stream_index,
     return ret;
 }
 
+int avio_dump_contents(AVIOContext *h, AVBPrint *pb)
+{
+    int ret;
+    char buf[1024];
+    while (1) {
+        ret = avio_read(h, buf, sizeof(buf));
+        if (ret == AVERROR_EOF)
+            return 0;
+        if (ret <= 0)
+            return ret;
+        av_bprint_append_data(pb, buf, ret);
+        if (!av_bprint_is_complete(pb))
+            return AVERROR(ENOMEM);
+    }
+}
+
 /* output in a dynamic buffer */
 
 typedef struct DynBuffer {
-- 
1.8.5.5



More information about the ffmpeg-devel mailing list