[FFmpeg-cvslog] SAMI demuxer and decoder.

Clément Bœsch git at videolan.org
Fri Jun 29 20:29:53 CEST 2012


ffmpeg | branch: master | Clément Bœsch <ubitux at gmail.com> | Sun Jun 17 11:42:47 2012 +0200| [53640f42be9f18368a8339116f8abf30616c60b2] | committer: Clément Bœsch

SAMI demuxer and decoder.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=53640f42be9f18368a8339116f8abf30616c60b2
---

 Changelog                |    1 +
 doc/general.texi         |    1 +
 libavcodec/Makefile      |    1 +
 libavcodec/allcodecs.c   |    1 +
 libavcodec/avcodec.h     |    1 +
 libavcodec/samidec.c     |  158 ++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h     |    4 +-
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    1 +
 libavformat/samidec.c    |  132 ++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |    2 +-
 tests/fate/subtitles.mak |    3 +
 tests/ref/fate/sub-sami  |    1 +
 13 files changed, 304 insertions(+), 3 deletions(-)

diff --git a/Changelog b/Changelog
index 5fc460b..5ea3ade 100644
--- a/Changelog
+++ b/Changelog
@@ -16,6 +16,7 @@ version next:
 - audio channel mapping filter
 - showwaves filter
 - LucasArts SMUSH playback support
+- SAMI demuxer and decoder
 
 
 version 0.11:
diff --git a/doc/general.texi b/doc/general.texi
index a55aa37..a1b1b01 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -864,6 +864,7 @@ performance on systems without hardware floating point support).
 @item JACOsub      @tab X @tab X @tab   @tab X
 @item MicroDVD     @tab X @tab X @tab   @tab X
 @item PGS          @tab   @tab   @tab   @tab X
+ at item SAMI         @tab   @tab X @tab   @tab X
 @item SubRip (SRT) @tab X @tab X @tab X @tab X
 @item XSUB         @tab   @tab   @tab X @tab X
 @end multitable
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 95bcb17..6c723db 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -393,6 +393,7 @@ OBJS-$(CONFIG_RV30_DECODER)            += rv30.o rv34.o rv30dsp.o rv34dsp.o \
                                           mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_RV40_DECODER)            += rv40.o rv34.o rv34dsp.o rv40dsp.o \
                                           mpegvideo.o error_resilience.o
+OBJS-$(CONFIG_SAMI_DECODER)            += samidec.o ass.o
 OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 8c2bd2b..be36b84 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -405,6 +405,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (JACOSUB, jacosub);
     REGISTER_DECODER (MICRODVD, microdvd);
     REGISTER_DECODER (PGSSUB, pgssub);
+    REGISTER_DECODER (SAMI, sami);
     REGISTER_ENCDEC  (SRT, srt);
     REGISTER_ENCDEC  (XSUB, xsub);
 
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 6f620f8..58243fd 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -430,6 +430,7 @@ enum CodecID {
     CODEC_ID_MICRODVD   = MKBETAG('m','D','V','D'),
     CODEC_ID_EIA_608    = MKBETAG('c','6','0','8'),
     CODEC_ID_JACOSUB    = MKBETAG('J','S','U','B'),
+    CODEC_ID_SAMI       = MKBETAG('S','A','M','I'),
 
     /* other specific kind of codecs (generally used for attachments) */
     CODEC_ID_FIRST_UNKNOWN = 0x18000,           ///< A dummy ID pointing at the start of various fake codecs.
diff --git a/libavcodec/samidec.c b/libavcodec/samidec.c
new file mode 100644
index 0000000..1852959
--- /dev/null
+++ b/libavcodec/samidec.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * SAMI subtitle decoder
+ * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
+ */
+
+#include "ass.h"
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
+
+typedef struct {
+    AVBPrint source;
+    AVBPrint content;
+    AVBPrint full;
+} SAMIContext;
+
+static int sami_paragraph_to_ass(AVCodecContext *avctx, const char *src)
+{
+    SAMIContext *sami = avctx->priv_data;
+    int ret = 0;
+    char *tag = NULL;
+    char *dupsrc = av_strdup(src);
+    char *p = dupsrc;
+
+    av_bprint_clear(&sami->content);
+    for (;;) {
+        char *saveptr = NULL;
+        int prev_chr_is_space = 0;
+        AVBPrint *dst = &sami->content;
+
+        /* parse & extract paragraph tag */
+        p = av_stristr(p, "<P");
+        if (!p)
+            break;
+        if (p[2] != '>' && !isspace(p[2])) { // avoid confusion with tags such as <PRE>
+            p++;
+            continue;
+        }
+        if (dst->len) // add a separator with the previous paragraph if there was one
+            av_bprintf(dst, "\\N");
+        tag = av_strtok(p, ">", &saveptr);
+        if (!tag || !saveptr)
+            break;
+        p = saveptr;
+
+        /* check if the current paragraph is the "source" (speaker name) */
+        if (av_stristr(tag, "ID=Source") || av_stristr(tag, "ID=\"Source\"")) {
+            dst = &sami->source;
+            av_bprint_clear(dst);
+        }
+
+        /* if empty event -> skip subtitle */
+        while (isspace(*p))
+            p++;
+        if (!strncmp(p, " ", 6)) {
+            ret = -1;
+            goto end;
+        }
+
+        /* extract the text, stripping most of the tags */
+        while (*p) {
+            if (*p == '<') {
+                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || isspace(p[2])))
+                    break;
+                if (!av_strncasecmp(p, "<BR", 3))
+                    av_bprintf(dst, "\\N");
+                p++;
+                while (*p && *p != '>')
+                    p++;
+                if (!*p)
+                    break;
+                if (*p == '>')
+                    p++;
+            }
+            if (!isspace(*p))
+                av_bprint_chars(dst, *p, 1);
+            else if (!prev_chr_is_space)
+                av_bprint_chars(dst, ' ', 1);
+            prev_chr_is_space = isspace(*p);
+            p++;
+        }
+    }
+
+    av_bprint_clear(&sami->full);
+    if (sami->source.len)
+        av_bprintf(&sami->full, "{\\i1}%s{\\i0}\\N", sami->source.str);
+    av_bprintf(&sami->full, "%s\r\n", sami->content.str);
+
+end:
+    av_free(dupsrc);
+    return ret;
+}
+
+static int sami_decode_frame(AVCodecContext *avctx,
+                             void *data, int *got_sub_ptr, AVPacket *avpkt)
+{
+    AVSubtitle *sub = data;
+    const char *ptr = avpkt->data;
+    SAMIContext *sami = avctx->priv_data;
+
+    if (ptr && avpkt->size > 0 && !sami_paragraph_to_ass(avctx, ptr)) {
+        int ts_start     = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){1,100});
+        int ts_duration  = avpkt->duration != -1 ?
+                           av_rescale_q(avpkt->duration, avctx->time_base, (AVRational){1,100}) : -1;
+        ff_ass_add_rect(sub, sami->full.str, ts_start, ts_duration, 0);
+    }
+    *got_sub_ptr = sub->num_rects > 0;
+    return avpkt->size;
+}
+
+static av_cold int sami_init(AVCodecContext *avctx)
+{
+    SAMIContext *sami = avctx->priv_data;
+    av_bprint_init(&sami->source,  0, 2048);
+    av_bprint_init(&sami->content, 0, 2048);
+    av_bprint_init(&sami->full,    0, 2048);
+    return ff_ass_subtitle_header_default(avctx);
+}
+
+static av_cold int sami_close(AVCodecContext *avctx)
+{
+    SAMIContext *sami = avctx->priv_data;
+    av_bprint_finalize(&sami->source,  NULL);
+    av_bprint_finalize(&sami->content, NULL);
+    av_bprint_finalize(&sami->full,    NULL);
+    return 0;
+}
+
+AVCodec ff_sami_decoder = {
+    .name           = "sami",
+    .long_name      = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = CODEC_ID_SAMI,
+    .priv_data_size = sizeof(SAMIContext),
+    .init           = sami_init,
+    .close          = sami_close,
+    .decode         = sami_decode_frame,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index bfc3b51..2c50064 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -27,8 +27,8 @@
  */
 
 #define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR  29
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MINOR  30
+#define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 2d78a81..96fb954 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -296,6 +296,7 @@ OBJS-$(CONFIG_RTPDEC)                    += rdt.o         \
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o rtspdec.o httpauth.o
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o \
                                             rtpenc_chain.o
+OBJS-$(CONFIG_SAMI_DEMUXER)              += samidec.o
 OBJS-$(CONFIG_SAP_DEMUXER)               += sapdec.o
 OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o rtpenc_chain.o
 OBJS-$(CONFIG_SBG_DEMUXER)               += sbgdec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index c8b9abf..4ae790f 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -206,6 +206,7 @@ void av_register_all(void)
     REGISTER_MUXDEMUX (RSO, rso);
     REGISTER_MUXDEMUX (RTP, rtp);
     REGISTER_MUXDEMUX (RTSP, rtsp);
+    REGISTER_DEMUXER  (SAMI, sami);
     REGISTER_MUXDEMUX (SAP, sap);
     REGISTER_DEMUXER  (SBG, sbg);
     REGISTER_DEMUXER  (SDP, sdp);
diff --git a/libavformat/samidec.c b/libavformat/samidec.c
new file mode 100644
index 0000000..5995d86
--- /dev/null
+++ b/libavformat/samidec.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * SAMI subtitle demuxer
+ * @see http://msdn.microsoft.com/en-us/library/ms971327.aspx
+ */
+
+#include "avformat.h"
+#include "internal.h"
+#include "subtitles.h"
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
+#include "libavutil/intreadwrite.h"
+
+typedef struct {
+    FFDemuxSubtitlesQueue q;
+} SAMIContext;
+
+static int sami_probe(AVProbeData *p)
+{
+    const unsigned char *ptr = p->buf;
+
+    if (AV_RB24(ptr) == 0xEFBBBF)
+        ptr += 3;  /* skip UTF-8 BOM */
+    return !strncmp(ptr, "<SAMI>", 6) ? AVPROBE_SCORE_MAX : 0;
+}
+
+static int sami_read_header(AVFormatContext *s)
+{
+    SAMIContext *sami = s->priv_data;
+    AVStream *st = avformat_new_stream(s, NULL);
+    AVBPrint buf, hdr_buf;
+    char c = 0;
+    int res = 0, got_first_sync_point = 0;
+
+    if (!st)
+        return AVERROR(ENOMEM);
+    avpriv_set_pts_info(st, 64, 1, 1000);
+    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+    st->codec->codec_id   = CODEC_ID_SAMI;
+
+    av_bprint_init(&buf,     0, AV_BPRINT_SIZE_UNLIMITED);
+    av_bprint_init(&hdr_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+    while (!url_feof(s->pb)) {
+        AVPacket *sub;
+        const int64_t pos = avio_tell(s->pb) - (c != 0);
+        int is_sync, n = ff_smil_extract_next_chunk(s->pb, &buf, &c);
+
+        if (n == 0)
+            break;
+
+        is_sync = !av_strncasecmp(buf.str, "<SYNC", 5);
+        if (is_sync)
+            got_first_sync_point = 1;
+
+        if (!got_first_sync_point) {
+            av_bprintf(&hdr_buf, "%s", buf.str);
+        } else {
+            sub = ff_subtitles_queue_insert(&sami->q, buf.str, buf.len, !is_sync);
+            if (!sub) {
+                res = AVERROR(ENOMEM);
+                goto end;
+            }
+            if (is_sync) {
+                const char *p = ff_smil_get_attr_ptr(buf.str, "Start");
+                sub->pos      = pos;
+                sub->pts      = p ? strtol(p, NULL, 10) : 0;
+                sub->duration = -1;
+            }
+        }
+        av_bprint_clear(&buf);
+    }
+
+    st->codec->extradata_size = hdr_buf.len + 1;
+    av_bprint_finalize(&hdr_buf, (char **)&st->codec->extradata);
+    if (!st->codec->extradata) {
+        st->codec->extradata_size = 0;
+        res = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    ff_subtitles_queue_finalize(&sami->q);
+
+end:
+    av_bprint_finalize(&buf, NULL);
+    return res;
+}
+
+static int sami_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    SAMIContext *sami = s->priv_data;
+    return ff_subtitles_queue_read_packet(&sami->q, pkt);
+}
+
+static int sami_read_close(AVFormatContext *s)
+{
+    SAMIContext *sami = s->priv_data;
+    ff_subtitles_queue_clean(&sami->q);
+    return 0;
+}
+
+AVInputFormat ff_sami_demuxer = {
+    .name           = "sami",
+    .long_name      = NULL_IF_CONFIG_SMALL("SAMI subtitle format"),
+    .priv_data_size = sizeof(SAMIContext),
+    .read_probe     = sami_probe,
+    .read_header    = sami_read_header,
+    .read_packet    = sami_read_packet,
+    .read_close     = sami_read_close,
+    .flags          = AVFMT_GENERIC_INDEX,
+    .extensions     = "smi,sami",
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 7057e2c..d5a9395 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR 54
-#define LIBAVFORMAT_VERSION_MINOR 12
+#define LIBAVFORMAT_VERSION_MINOR 13
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
diff --git a/tests/fate/subtitles.mak b/tests/fate/subtitles.mak
index 4e79abd..538e352 100644
--- a/tests/fate/subtitles.mak
+++ b/tests/fate/subtitles.mak
@@ -4,6 +4,9 @@ fate-sub-jacosub: CMD = md5 -i $(SAMPLES)/sub/JACOsub_capability_tester.jss -f a
 FATE_SUBTITLES += fate-sub-microdvd
 fate-sub-microdvd: CMD = md5 -i $(SAMPLES)/sub/MicroDVD_capability_tester.sub -f ass
 
+FATE_SUBTITLES += fate-sub-sami
+fate-sub-sami: CMD = md5 -i $(SAMPLES)/sub/SAMI_capability_tester.smi -f ass
+
 FATE_SUBTITLES += fate-sub-srt
 fate-sub-srt: CMD = md5 -i $(SAMPLES)/sub/SubRip_capability_tester.srt -f ass
 
diff --git a/tests/ref/fate/sub-sami b/tests/ref/fate/sub-sami
new file mode 100644
index 0000000..308e000
--- /dev/null
+++ b/tests/ref/fate/sub-sami
@@ -0,0 +1 @@
+00642e143339b4ca29dc6e990436387c



More information about the ffmpeg-cvslog mailing list