[FFmpeg-devel] [PATCH 09/11] lavf/subtitles: add some SMIL helpers.

Clément Bœsch ubitux at gmail.com
Fri Jun 22 22:44:04 CEST 2012


This is needed for SAMI and RealText demuxers.
---
 libavformat/subtitles.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 libavformat/subtitles.h | 16 ++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index ce6cc35..e1667cc 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -20,6 +20,7 @@
 
 #include "avformat.h"
 #include "subtitles.h"
+#include "libavutil/avstring.h"
 
 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
                                     const uint8_t *event, int len, int merge)
@@ -99,3 +100,46 @@ void ff_subtitles_queue_free(FFDemuxSubtitlesQueue *q)
     av_freep(&q->subs);
     q->nsub = q->allocated_size = q->current_sub = 0;
 }
+
+int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
+{
+    int i = 0;
+    char end_chr;
+
+    if (!*c) // cached char?
+        *c = avio_r8(pb);
+    if (!*c)
+        return 0;
+
+    end_chr = *c == '<' ? '>' : '<';
+    do {
+        av_bprint_chars(buf, *c, 1);
+        *c = avio_r8(pb);
+        i++;
+    } while (*c != end_chr && *c);
+    if (end_chr == '>') {
+        av_bprint_chars(buf, '>', 1);
+        *c = 0;
+    }
+    return i;
+}
+
+const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
+{
+    int in_quotes = 0;
+    const int len = strlen(attr);
+
+    while (*s) {
+        while (*s) {
+            if (!in_quotes && isspace(*s))
+                break;
+            in_quotes ^= *s == '"'; // XXX: support escaping?
+            s++;
+        }
+        while (isspace(*s))
+            s++;
+        if (!av_strncasecmp(s, attr, len) && s[len] == '=')
+            return s + len + 1 + (s[len + 1] == '"');
+    }
+    return NULL;
+}
diff --git a/libavformat/subtitles.h b/libavformat/subtitles.h
index 3373027..4e7b2ea 100644
--- a/libavformat/subtitles.h
+++ b/libavformat/subtitles.h
@@ -23,6 +23,7 @@
 
 #include <stdint.h>
 #include "avformat.h"
+#include "libavutil/bprint.h"
 
 typedef struct {
     AVPacket *subs;         ///< array of subtitles packets
@@ -58,4 +59,19 @@ int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt);
  */
 void ff_subtitles_queue_free(FFDemuxSubtitlesQueue *q);
 
+/**
+ * SMIL helper to load next chunk ("<...>" or untagged content) in buf
+ *
+ * @param c cached character, to avoid a backward seek
+ */
+int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c);
+
+/**
+ * SMIL helper to point on the value of an attribute in the given tag
+ *
+ * @param s    SMIL tag ("<...>")
+ * @param attr the attribute to look for
+ */
+const char *ff_smil_get_attr_ptr(const char *s, const char *attr);
+
 #endif /* AVFORMAT_SUBTITLES_H */
-- 
1.7.11



More information about the ffmpeg-devel mailing list