Browse code

Metadata demuxer.

Originally committed as revision 26102 to svn://svn.ffmpeg.org/ffmpeg/trunk

Anton Khirnov authored on 2010/12/27 16:46:53
Showing 6 changed files
... ...
@@ -68,6 +68,7 @@ version <next>:
68 68
 - AMR-WB decoder
69 69
 - replace the ocv_smooth filter with a more generic ocv filter
70 70
 - Windows Televison (WTV) demuxer
71
+- FFmpeg metadata format muxer and demuxer
71 72
 
72 73
 
73 74
 version 0.6:
... ...
@@ -118,7 +118,7 @@ library:
118 118
     @tab VR native stream format, used by Leitch/Harris' video servers.
119 119
 @item Matroska                  @tab X @tab X
120 120
 @item Matroska audio            @tab X @tab
121
-@item FFmpeg metadata           @tab X @tab
121
+@item FFmpeg metadata           @tab X @tab X
122 122
     @tab Metadata in text format.
123 123
 @item MAXIS XA                  @tab   @tab X
124 124
     @tab Used in Sim City 3000; file extension .xa.
... ...
@@ -406,6 +406,7 @@ enum CodecID {
406 406
 
407 407
     CODEC_ID_MPEG2TS= 0x20000, /**< _FAKE_ codec to indicate a raw MPEG-2 TS
408 408
                                 * stream (only used by libavformat) */
409
+    CODEC_ID_FFMETADATA=0x21000,   ///< Dummy codec for streams containing only metadata information.
409 410
 };
410 411
 
411 412
 #if LIBAVCODEC_VERSION_MAJOR < 53
... ...
@@ -71,6 +71,7 @@ OBJS-$(CONFIG_EAC3_DEMUXER)              += ac3dec.o rawdec.o
71 71
 OBJS-$(CONFIG_EAC3_MUXER)                += rawenc.o
72 72
 OBJS-$(CONFIG_FFM_DEMUXER)               += ffmdec.o
73 73
 OBJS-$(CONFIG_FFM_MUXER)                 += ffmenc.o
74
+OBJS-$(CONFIG_FFMETADATA_DEMUXER)        += metadec.o
74 75
 OBJS-$(CONFIG_FFMETADATA_MUXER)          += metaenc.o
75 76
 OBJS-$(CONFIG_FILMSTRIP_DEMUXER)         += filmstripdec.o
76 77
 OBJS-$(CONFIG_FILMSTRIP_MUXER)           += filmstripenc.o
... ...
@@ -85,7 +85,7 @@ void av_register_all(void)
85 85
     REGISTER_DEMUXER  (EA_CDATA, ea_cdata);
86 86
     REGISTER_MUXDEMUX (EAC3, eac3);
87 87
     REGISTER_MUXDEMUX (FFM, ffm);
88
-    REGISTER_MUXER    (FFMETADATA, ffmetadata);
88
+    REGISTER_MUXDEMUX (FFMETADATA, ffmetadata);
89 89
     REGISTER_MUXDEMUX (FILMSTRIP, filmstrip);
90 90
     REGISTER_MUXDEMUX (FLAC, flac);
91 91
     REGISTER_DEMUXER  (FLIC, flic);
92 92
new file mode 100644
... ...
@@ -0,0 +1,172 @@
0
+/*
1
+ * Metadata demuxer
2
+ * Copyright (c) 2010 Anton Khirnov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "meta.h"
23
+
24
+static int probe(AVProbeData *p)
25
+{
26
+    if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
27
+        return AVPROBE_SCORE_MAX;
28
+    return 0;
29
+}
30
+
31
+static void get_line(ByteIOContext *s, uint8_t *buf, int size)
32
+{
33
+    do {
34
+        uint8_t c;
35
+        int i = 0;
36
+
37
+        while ((c = get_byte(s))) {
38
+            if (c == '\\') {
39
+                if (i < size - 1)
40
+                    buf[i++] = c;
41
+                c = get_byte(s);
42
+            } else if (c == '\n')
43
+                break;
44
+
45
+            if (i < size - 1)
46
+                buf[i++] = c;
47
+        }
48
+        buf[i] = 0;
49
+    } while (!url_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
50
+}
51
+
52
+static AVChapter *read_chapter(AVFormatContext *s)
53
+{
54
+    uint8_t line[256];
55
+    int64_t start, end;
56
+    AVRational tb = {1, 1e9};
57
+
58
+    get_line(s->pb, line, sizeof(line));
59
+
60
+    if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
61
+        get_line(s->pb, line, sizeof(line));
62
+    if (!sscanf(line, "START=%lld", &start)) {
63
+        av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
64
+        start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
65
+                 s->chapters[s->nb_chapters - 1]->end : 0;
66
+    } else
67
+        get_line(s->pb, line, sizeof(line));
68
+
69
+    if (!sscanf(line, "END=%lld", &end)) {
70
+        av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
71
+        end = AV_NOPTS_VALUE;
72
+    }
73
+
74
+    return ff_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
75
+}
76
+
77
+static uint8_t *unescape(uint8_t *buf, int size)
78
+{
79
+    uint8_t *ret = av_malloc(size + 1);
80
+    uint8_t *p1  = ret, *p2 = buf;
81
+
82
+    if (!ret)
83
+        return NULL;
84
+
85
+    while (p2 < buf + size) {
86
+        if (*p2 == '\\')
87
+            p2++;
88
+        *p1++ = *p2++;
89
+    }
90
+    *p1 = 0;
91
+    return ret;
92
+}
93
+
94
+static int read_tag(uint8_t *line, AVMetadata **m)
95
+{
96
+    uint8_t *key, *value, *p = line;
97
+
98
+    /* find first not escaped '=' */
99
+    while (1) {
100
+        if (*p == '=')
101
+            break;
102
+        else if (*p == '\\')
103
+            p++;
104
+
105
+        if (*p++)
106
+            continue;
107
+
108
+        return 0;
109
+    }
110
+
111
+    if (!(key = unescape(line, p - line)))
112
+        return AVERROR(ENOMEM);
113
+    if (!(value = unescape(p + 1, strlen(p + 1)))) {
114
+        av_free(key);
115
+        return AVERROR(ENOMEM);
116
+    }
117
+
118
+    av_metadata_set2(m, key, value, AV_METADATA_DONT_STRDUP_KEY | AV_METADATA_DONT_STRDUP_VAL);
119
+    return 0;
120
+}
121
+
122
+static int read_header(AVFormatContext *s, AVFormatParameters *ap)
123
+{
124
+    AVMetadata **m = &s->metadata;
125
+    uint8_t line[1024];
126
+
127
+    while(!url_feof(s->pb)) {
128
+        get_line(s->pb, line, sizeof(line));
129
+
130
+        if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
131
+            AVStream *st = av_new_stream(s, 0);
132
+
133
+            if (!st)
134
+                return -1;
135
+
136
+            st->codec->codec_type = AVMEDIA_TYPE_DATA;
137
+            st->codec->codec_id   = CODEC_ID_FFMETADATA;
138
+
139
+            m = &st->metadata;
140
+        } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
141
+            AVChapter *ch = read_chapter(s);
142
+
143
+            if (!ch)
144
+                return -1;
145
+
146
+            m = &ch->metadata;
147
+        } else
148
+            read_tag(line, m);
149
+    }
150
+
151
+    s->start_time = 0;
152
+    if (s->nb_chapters)
153
+        s->duration = av_rescale_q(s->chapters[s->nb_chapters - 1]->end,
154
+                                   s->chapters[s->nb_chapters - 1]->time_base,
155
+                                   AV_TIME_BASE_Q);
156
+
157
+    return 0;
158
+}
159
+
160
+static int read_packet(AVFormatContext *s, AVPacket *pkt)
161
+{
162
+    return AVERROR_EOF;
163
+}
164
+
165
+AVInputFormat ffmetadata_demuxer = {
166
+    .name        = "ffmetadata",
167
+    .long_name   = NULL_IF_CONFIG_SMALL("FFmpeg metadata in text format"),
168
+    .read_probe  = probe,
169
+    .read_header = read_header,
170
+    .read_packet = read_packet,
171
+};