Browse code

lavf: add data: URI scheme.

Nicolas George authored on 2012/12/27 04:26:08
Showing 6 changed files
... ...
@@ -53,6 +53,7 @@ version <next>:
53 53
 - MPL2, VPlayer, MPlayer, AQTitle, PJS and SubViewer v1 subtitles demuxers and decoders
54 54
 - Sony Wave64 muxer
55 55
 - adobe and limelight publisher authentication in RTMP
56
+- data: URI scheme
56 57
 
57 58
 
58 59
 version 1.0:
... ...
@@ -75,6 +75,15 @@ ffplay concat:split1.mpeg\|split2.mpeg\|split3.mpeg
75 75
 Note that you may need to escape the character "|" which is special for
76 76
 many shells.
77 77
 
78
+@section data
79
+
80
+Data in-line in the URI. See @url{http://en.wikipedia.org/wiki/Data_URI_scheme}.
81
+
82
+For example, to convert a GIF file given inline with @command{ffmpeg}:
83
+@example
84
+ffmpeg -i "data:image/gif;base64,R0lGODdhCAAIAMIEAAAAAAAA//8AAP//AP///////////////ywAAAAACAAIAAADF0gEDLojDgdGiJdJqUX02iB4E8Q9jUMkADs=" smiley.png
85
+@end example
86
+
78 87
 @section file
79 88
 
80 89
 File access protocol.
... ...
@@ -408,6 +408,7 @@ OBJS-$(CONFIG_BLURAY_PROTOCOL)           += bluray.o
408 408
 OBJS-$(CONFIG_CACHE_PROTOCOL)            += cache.o
409 409
 OBJS-$(CONFIG_CONCAT_PROTOCOL)           += concat.o
410 410
 OBJS-$(CONFIG_CRYPTO_PROTOCOL)           += crypto.o
411
+OBJS-$(CONFIG_DATA_PROTOCOL)             += data_uri.o
411 412
 OBJS-$(CONFIG_FFRTMPCRYPT_PROTOCOL)      += rtmpcrypt.o rtmpdh.o
412 413
 OBJS-$(CONFIG_FFRTMPHTTP_PROTOCOL)       += rtmphttp.o
413 414
 OBJS-$(CONFIG_FILE_PROTOCOL)             += file.o
... ...
@@ -306,6 +306,7 @@ void av_register_all(void)
306 306
     REGISTER_PROTOCOL(CACHE,            cache);
307 307
     REGISTER_PROTOCOL(CONCAT,           concat);
308 308
     REGISTER_PROTOCOL(CRYPTO,           crypto);
309
+    REGISTER_PROTOCOL(DATA,             data);
309 310
     REGISTER_PROTOCOL(FFRTMPCRYPT,      ffrtmpcrypt);
310 311
     REGISTER_PROTOCOL(FFRTMPHTTP,       ffrtmphttp);
311 312
     REGISTER_PROTOCOL(FILE,             file);
312 313
new file mode 100644
... ...
@@ -0,0 +1,118 @@
0
+/*
1
+ * Copyright (c) 2012 Nicolas George
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public License
7
+ * as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <string.h>
21
+#include "libavutil/avstring.h"
22
+#include "libavutil/base64.h"
23
+#include "url.h"
24
+
25
+typedef struct {
26
+    const uint8_t *data;
27
+    void *tofree;
28
+    size_t size;
29
+    size_t pos;
30
+} DataContext;
31
+
32
+static av_cold int data_open(URLContext *h, const char *uri, int flags)
33
+{
34
+    DataContext *dc = h->priv_data;
35
+    const char *data, *opt, *next;
36
+    char *ddata;
37
+    int ret, base64 = 0;
38
+    size_t in_size;
39
+
40
+    /* data:content/type[;base64],payload */
41
+
42
+    av_strstart(uri, "data:", &uri);
43
+    data = strchr(uri, ',');
44
+    if (!data) {
45
+        av_log(h, AV_LOG_ERROR, "No ',' delimiter in URI\n");
46
+        return AVERROR(EINVAL);
47
+    }
48
+    opt = uri;
49
+    while (opt < data) {
50
+        next = av_x_if_null(memchr(opt, ';', data - opt), data);
51
+        if (opt == uri) {
52
+            if (!memchr(opt, '/', next - opt)) { /* basic validity check */
53
+                av_log(h, AV_LOG_ERROR, "Invalid content-type '%.*s'\n",
54
+                       (int)(next - opt), opt);
55
+                return AVERROR(EINVAL);
56
+            }
57
+            av_log(h, AV_LOG_VERBOSE, "Content-type: %.*s\n",
58
+                   (int)(next - opt), opt);
59
+        } else {
60
+            if (!av_strncasecmp(opt, "base64", next - opt)) {
61
+                base64 = 1;
62
+            } else {
63
+                av_log(h, AV_LOG_VERBOSE, "Ignoring option '%.*s'\n",
64
+                       (int)(next - opt), opt);
65
+            }
66
+        }
67
+        opt = next + 1;
68
+    }
69
+
70
+    data++;
71
+    in_size = strlen(data);
72
+    if (base64) {
73
+        size_t out_size = 3 * (in_size / 4) + 1;
74
+
75
+        if (out_size > INT_MAX || !(ddata = av_malloc(out_size)))
76
+            return AVERROR(ENOMEM);
77
+        if ((ret = av_base64_decode(ddata, data, out_size)) < 0) {
78
+            av_free(ddata);
79
+            av_log(h, AV_LOG_ERROR, "Invalid base64 in URI\n");
80
+            return ret;
81
+        }
82
+        dc->data = dc->tofree = ddata;
83
+        dc->size = ret;
84
+    } else {
85
+        dc->data = data;
86
+        dc->size = in_size;
87
+    }
88
+    return 0;
89
+}
90
+
91
+static av_cold int data_close(URLContext *h)
92
+{
93
+    DataContext *dc = h->priv_data;
94
+
95
+    av_freep(&dc->tofree);
96
+    return 0;
97
+}
98
+
99
+static int data_read(URLContext *h, unsigned char *buf, int size)
100
+{
101
+    DataContext *dc = h->priv_data;
102
+
103
+    if (dc->pos >= dc->size)
104
+        return AVERROR_EOF;
105
+    size = FFMIN(size, dc->size - dc->pos);
106
+    memcpy(buf, dc->data + dc->pos, size);
107
+    dc->pos += size;
108
+    return size;
109
+}
110
+
111
+URLProtocol ff_data_protocol = {
112
+    .name           = "data",
113
+    .url_open       = data_open,
114
+    .url_close      = data_close,
115
+    .url_read       = data_read,
116
+    .priv_data_size = sizeof(DataContext),
117
+};
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 54
33
-#define LIBAVFORMAT_VERSION_MINOR 58
34
-#define LIBAVFORMAT_VERSION_MICRO 102
33
+#define LIBAVFORMAT_VERSION_MINOR 59
34
+#define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \