[FFmpeg-devel] [PATCH 1/3] lavf/subtitles: add some SMIL helpers.

Clément Bœsch ubitux at gmail.com
Sun Jun 17 22:30:54 CEST 2012


On Sun, Jun 17, 2012 at 03:16:19PM +0200, Nicolas George wrote:
> Le decadi 30 prairial, an CCXX, Clément Bœsch a écrit :
> > This is needed for SAMI and RealText demuxers.
> > ---
> >  libavformat/subtitles.c |   35 +++++++++++++++++++++++++++++++++++
> >  libavformat/subtitles.h |   13 +++++++++++++
> >  2 files changed, 48 insertions(+)
> > 
> > diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
> > index 5e3cdee..39e18f8 100644
> > --- a/libavformat/subtitles.c
> > +++ b/libavformat/subtitles.c
> > @@ -20,6 +20,7 @@
> >  
> >  #include "avformat.h"
> >  #include "subtitles.h"
> > +#include "libavutil/avstring.h"
> >  
> >  FFDemuxSubEntry *ff_subtitles_queue_insert_event(FFDemuxSubtitlesQueue *q,
> >                                                   const uint8_t *event, int len,
> > @@ -105,3 +106,37 @@ void ff_subtitles_queue_free(FFDemuxSubtitlesQueue *q)
> >      av_freep(&q->subs);
> >      q->nsub = q->allocated_size = 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;
> > +}
> > +
> > +char *ff_smil_get_attr_ptr(const char *s, const char *tag)
> > +{
> > +    char *p = av_stristr(s, tag);
> > +    if (p) {
> > +        p += strlen(tag);
> > +        if (*p == '=') p++;
> > +        if (*p == '"') p++;
> > +    }
> > +    return p;
> > +}
> 
> Unless I am mistaken, ff_smil_get_attr_ptr(s, "x") will find the "index"
> too.
> 

Yeah that was lame, quick and dirty. Should be better with the new
attached version.

> > diff --git a/libavformat/subtitles.h b/libavformat/subtitles.h
> > index c098131..be875cd 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 {
> >      uint8_t *event;         ///< allocated subtitle line, zero terminated
> > @@ -67,4 +68,16 @@ int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q,
> >   */
> >  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 a given tag
> > + */
> 
> I find that comment rather obscure. Especially since it looks like you are
> scanning for what is called, in HTML, an attribute and not a tag.
> 

Herp derp indeed. Fixed.

> > +char *ff_smil_get_attr_ptr(const char *s, const char *tag);
> > +
> >  #endif /* AVFORMAT_SUBTITLES_H */

[...]

-- 
Clément B.
-------------- next part --------------
From 3de51606ca7fa119c6ec59f90464ecf9466b4e21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Sun, 17 Jun 2012 11:43:09 +0200
Subject: [PATCH 08/10] lavf/subtitles: add some SMIL helpers.

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

diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 8012039..6f13665 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -20,6 +20,7 @@
 
 #include "avformat.h"
 #include "subtitles.h"
+#include "libavutil/avstring.h"
 
 FFDemuxSubEntry *ff_subtitles_queue_insert_event(FFDemuxSubtitlesQueue *q,
                                                  const uint8_t *event, int len,
@@ -110,3 +111,41 @@ 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)
+{
+    const int len = strlen(attr);
+
+    while (*s) {
+        while (*s && !isspace(*s))
+            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 dfe0d6c..16657e0 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 {
     uint8_t *event;         ///< allocated subtitle line, zero terminated
@@ -67,4 +68,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.10.4

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120617/f3f31462/attachment.asc>


More information about the ffmpeg-devel mailing list