[FFmpeg-devel] [PATCH 3/3] Metadata demuxer.

Anton Khirnov anton
Wed Dec 15 13:15:14 CET 2010


---
 doc/general.texi         |    2 +-
 libavcodec/avcodec.h     |    1 +
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    2 +-
 libavformat/metadec.c    |  173 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 177 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/metadec.c

diff --git a/doc/general.texi b/doc/general.texi
index 410b8fa..582920c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -118,7 +118,7 @@ library:
     @tab VR native stream format, used by Leitch/Harris' video servers.
 @item Matroska                  @tab X @tab X
 @item Matroska audio            @tab X @tab
- at item FFmpeg metadata           @tab X @tab
+ at item FFmpeg metadata           @tab X @tab X
     @tab Metadata in text format.
 @item MAXIS XA                  @tab   @tab X
     @tab Used in Sim City 3000; file extension .xa.
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index ecfc28b..7feaba1 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -406,6 +406,7 @@ enum CodecID {
 
     CODEC_ID_MPEG2TS= 0x20000, /**< _FAKE_ codec to indicate a raw MPEG-2 TS
                                 * stream (only used by libavformat) */
+    CODEC_ID_META=0x21000,   ///< Dummy codec for streams containing only metadata information.
 };
 
 #if LIBAVCODEC_VERSION_MAJOR < 53
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 629e796..c1de14f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -116,6 +116,7 @@ OBJS-$(CONFIG_MATROSKA_MUXER)            += matroskaenc.o matroska.o \
                                             riff.o isom.o avc.o \
                                             flacenc_header.o avlanguage.o
 OBJS-$(CONFIG_MD5_MUXER)                 += md5enc.o
+OBJS-$(CONFIG_METADATA_DEMUXER)          += metadec.o
 OBJS-$(CONFIG_METADATA_MUXER)            += metaenc.o
 OBJS-$(CONFIG_MJPEG_DEMUXER)             += rawdec.o
 OBJS-$(CONFIG_MJPEG_MUXER)               += rawenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index a406dba..5599311 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -115,7 +115,7 @@ void av_register_all(void)
     REGISTER_MUXER    (MD5, md5);
     REGISTER_MUXDEMUX (MATROSKA, matroska);
     REGISTER_MUXER    (MATROSKA_AUDIO, matroska_audio);
-    REGISTER_MUXER    (METADATA, metadata);
+    REGISTER_MUXDEMUX (METADATA, metadata);
     REGISTER_MUXDEMUX (MJPEG, mjpeg);
     REGISTER_MUXDEMUX (MLP, mlp);
     REGISTER_DEMUXER  (MM, mm);
diff --git a/libavformat/metadec.c b/libavformat/metadec.c
new file mode 100644
index 0000000..b149a02
--- /dev/null
+++ b/libavformat/metadec.c
@@ -0,0 +1,173 @@
+/*
+ * Metadata demuxer
+ * Copyright (c) 2010 Anton Khirnov
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "meta.h"
+
+static int probe(AVProbeData *p)
+{
+    if(!memcmp(p->buf, ID_STRING, strlen(ID_STRING)))
+        return AVPROBE_SCORE_MAX;
+    return 0;
+}
+
+static void get_line(ByteIOContext *s, uint8_t *buf, int size)
+{
+    do {
+        uint8_t c;
+        int i = 0;
+
+        while ((c = get_byte(s))) {
+            if (c == '\\') {
+                if (i < size - 1)
+                    buf[i++] = c;
+                c = get_byte(s);
+            } else if (c == '\n')
+                break;
+
+            if (i < size - 1)
+                buf[i++] = c;
+        }
+        buf[i] = 0;
+    } while (!url_feof(s) && (buf[0] == ';' || buf[0] == '#' || buf[0] == 0));
+}
+
+static AVChapter *read_chapter(AVFormatContext *s)
+{
+    uint8_t line[256];
+    int64_t start, end;
+    AVRational tb = {1, 1e9};
+
+    get_line(s->pb, line, sizeof(line));
+
+    if (sscanf(line, "TIMEBASE=%d/%d", &tb.num, &tb.den))
+        get_line(s->pb, line, sizeof(line));
+    if (!sscanf(line, "START=%lld", &start)) {
+        av_log(s, AV_LOG_ERROR, "Expected chapter start timestamp, found %s.\n", line);
+        start = (s->nb_chapters && s->chapters[s->nb_chapters - 1]->end != AV_NOPTS_VALUE) ?
+                 s->chapters[s->nb_chapters - 1]->end : 0;
+    } else
+        get_line(s->pb, line, sizeof(line));
+
+    if (!sscanf(line, "END=%lld", &end)) {
+        av_log(s, AV_LOG_ERROR, "Expected chapter end timestamp, found %s.\n", line);
+        end = AV_NOPTS_VALUE;
+    }
+
+    return ff_new_chapter(s, s->nb_chapters, tb, start, end, NULL);
+}
+
+static int read_tag(uint8_t *line, AVMetadata **m)
+{
+    uint8_t *key, *value, *p = line, *p1, *p2;
+
+    /* find first not escaped '=' */
+    while (1) {
+        if (*p == '=')
+            break;
+        else if (*p == '\\')
+            p++;
+
+        if (*p++)
+            continue;
+
+        return 0;
+    }
+
+    if (!(key   = av_malloc((p - line + 1) * sizeof(*p))))
+       return AVERROR(ENOMEM);
+    if (!(value = av_malloc((strlen(p)) * sizeof(*p)))) {
+        av_free(key);
+        return AVERROR(ENOMEM);
+    }
+
+    /* unescape key/value */
+    p1 = key;
+    p2 = line;
+    while(p2 < p) {
+        if (*p2 == '\\')
+            p2++;
+        *p1++ = *p2++;
+    }
+    *p1 = 0;
+
+    p1 = value;
+    p2 = p + 1;
+    while(*p2) {
+        if (*p2 == '\\')
+            p2++;
+        *p1++ = *p2++;
+    }
+    *p1 = 0;
+
+    av_metadata_set2(m, key, value, AV_METADATA_DONT_STRDUP_KEY | AV_METADATA_DONT_STRDUP_VAL);
+    return 0;
+}
+
+static int read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+    AVMetadata **m = &s->metadata;
+    uint8_t line[1024];
+
+    while(!url_feof(s->pb)) {
+        get_line(s->pb, line, sizeof(line));
+
+        if (!memcmp(line, ID_STREAM, strlen(ID_STREAM))) {
+            AVStream *st = av_new_stream(s, 0);
+
+            if (!st)
+                return -1;
+
+            st->codec->codec_type = AVMEDIA_TYPE_DATA;
+            st->codec->codec_id   = CODEC_ID_META;
+
+            m = &st->metadata;
+        } else if (!memcmp(line, ID_CHAPTER, strlen(ID_CHAPTER))) {
+            AVChapter *ch = read_chapter(s);
+
+            if (!ch)
+                return -1;
+
+            m = &ch->metadata;
+        } else
+            read_tag(line, m);
+    }
+
+    s->start_time = 0;
+    if (s->nb_chapters)
+        s->duration = s->chapters[s->nb_chapters - 1]->end;
+
+    return 0;
+}
+
+static int read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    return AVERROR_EOF;
+}
+
+AVInputFormat metadata_demuxer = {
+    "metadata",
+    NULL_IF_CONFIG_SMALL("Metadata in text format"),
+    0,
+    probe,
+    read_header,
+    read_packet,
+};
-- 
1.7.2.3




More information about the ffmpeg-devel mailing list