[FFmpeg-devel] [PATCH][WIP] JACOsub demuxer, decoder and muxer.

Clément Bœsch ubitux at gmail.com
Fri Feb 3 13:03:43 CET 2012


---
Hi,

Again, another work in progress. I started this one year ago, aiming to support
all the subtitles format MPlayer currently supports, and of course it's still
not done (but likely working).

That format isn't really useful; it's somehow an ancestor of ASS with crazy
features (not supported here) like includes, bitmap blit, playback control, or
insane markups. Since it covers a bunch of tricky things subtitles can have, I
though it would be a good learning exercise; this way I may understand better
how they should be integrated in the libavfilter chain.

See @todo in demuxer for stuff I'd like to support before having it upstream.
---
 MAINTAINERS              |    1 +
 doc/general.texi         |    1 +
 libavcodec/Makefile      |    1 +
 libavcodec/allcodecs.c   |    1 +
 libavcodec/avcodec.h     |    1 +
 libavcodec/jacosub.h     |   65 ++++++++++++
 libavcodec/jacosubdec.c  |  261 ++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h     |    4 +-
 libavformat/Makefile     |    2 +
 libavformat/allformats.c |    1 +
 libavformat/jacosubdec.c |  220 ++++++++++++++++++++++++++++++++++++++
 libavformat/rawenc.c     |   12 ++
 libavformat/version.h    |    2 +-
 13 files changed, 569 insertions(+), 3 deletions(-)
 create mode 100644 libavcodec/jacosub.h
 create mode 100644 libavcodec/jacosubdec.c
 create mode 100644 libavformat/jacosubdec.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 7a04209..9da7ab7 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -313,6 +313,7 @@ Muxers/Demuxers:
   ipmovie.c                             Mike Melanson
   img2.c                                Michael Niedermayer
   iss.c                                 Stefan Gehrer
+  jacosub*                              Clément Bœsch
   jvdec.c                               Peter Ross
   libmodplug.c                          Clément Bœsch
   libnut.c                              Oded Shimon
diff --git a/doc/general.texi b/doc/general.texi
index a183829..3f5d422 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -821,6 +821,7 @@ performance on systems without hardware floating point support).
 @item SSA/ASS      @tab X @tab X @tab X @tab X
 @item DVB          @tab X @tab X @tab X @tab X
 @item DVD          @tab X @tab X @tab X @tab X
+ at item JACOsub      @tab X @tab X @tab   @tab X
 @item MicroDVD     @tab X @tab X @tab   @tab
 @item PGS          @tab   @tab   @tab   @tab X
 @item SubRip (SRT) @tab X @tab X @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2173855..fb0b860 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -215,6 +215,7 @@ OBJS-$(CONFIG_INDEO4_DECODER)          += indeo4.o ivi_common.o ivi_dsp.o
 OBJS-$(CONFIG_INDEO5_DECODER)          += indeo5.o ivi_common.o ivi_dsp.o
 OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o
 OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
+OBJS-$(CONFIG_JACOSUB_DECODER)         += jacosubdec.o ass.o
 OBJS-$(CONFIG_JPEG2000_DECODER)        += j2kdec.o mqcdec.o mqc.o j2k.o j2k_dwt.o
 OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o mqcenc.o mqc.o j2k.o j2k_dwt.o
 OBJS-$(CONFIG_JPEGLS_DECODER)          += jpeglsdec.o jpegls.o \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 06b872c..1c22ef3 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -388,6 +388,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC  (ASS, ass);
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
+    REGISTER_DECODER (JACOSUB, jacosub)
     REGISTER_DECODER (PGSSUB, pgssub);
     REGISTER_ENCDEC  (SRT, srt);
     REGISTER_ENCDEC  (XSUB, xsub);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 8db0410..69c975b 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -407,6 +407,7 @@ enum CodecID {
     CODEC_ID_DVB_TELETEXT,
     CODEC_ID_SRT,
     CODEC_ID_MICRODVD   = MKBETAG('m','D','V','D'),
+    CODEC_ID_JACOSUB    = MKBETAG('J','S','U','B'),
 
     /* 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/jacosub.h b/libavcodec/jacosub.h
new file mode 100644
index 0000000..1ac49b3
--- /dev/null
+++ b/libavcodec/jacosub.h
@@ -0,0 +1,65 @@
+/*
+ * 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
+ * JACOsub shared utils
+ */
+
+#ifndef AVCODEC_JACOSUB_H
+#define AVCODEC_JACOSUB_H
+
+#define JSS_MAX_LINESIZE 512
+
+static av_always_inline int jss_whitespace(char c)
+{
+    return c == ' ' || (c >= '\t' && c <= '\r');
+}
+
+static av_always_inline const char *jss_skip_whitespace(const char *p)
+{
+    while (jss_whitespace(*p))
+        p++;
+    return p;
+}
+
+static av_always_inline int jss_get_shift(int timeres, const char *buf)
+{
+    int sign = 1;
+    int a = 0, b = 0, c = 0, d = 0;
+#define SSEP "%*1[.:]"
+    int n = sscanf(buf, "%d"SSEP"%d"SSEP"%d"SSEP"%d", &a, &b, &c, &d);
+#undef SSEP
+
+    if (*buf == '-' || a < 0) {
+        sign = -1;
+        a = FFABS(a);
+    }
+
+    switch (n) {
+    case 4: return sign * ((a*3600 + b*60 + c) * timeres + d);
+    case 3: return sign * ((         a*60 + b) * timeres + c);
+    case 2: return sign * ((                a) * timeres + b);
+    }
+
+    return 0;
+}
+
+#endif /* AVCODEC_JACOSUB_H */
diff --git a/libavcodec/jacosubdec.c b/libavcodec/jacosubdec.c
new file mode 100644
index 0000000..73f0f3f
--- /dev/null
+++ b/libavcodec/jacosubdec.c
@@ -0,0 +1,261 @@
+/*
+ * 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
+ * JACOsub subtitle decoder
+ * @see http://unicorn.us.com/jacosub/jscripts.html
+ */
+
+#include <time.h>
+#include "ass.h"
+#include "jacosub.h"
+#include "libavutil/avstring.h"
+
+#undef time
+
+typedef struct {
+    int shift;
+    unsigned timeres;
+} JACOsubContext;
+
+#define INSERT_STR(str) dst += snprintf(dst, dst_end - dst, "%s", str)
+#define CODES_MAP_ARGS char *dst, char *dst_end, const char **in, const char *arg
+
+static char *insert_text(CODES_MAP_ARGS)
+{
+    INSERT_STR(arg);
+    return dst;
+}
+
+static char *insert_datetime(CODES_MAP_ARGS)
+{
+    char buf[16];
+    time_t now = time(0);
+    struct tm ltime;
+
+#if HAVE_LOCALTIME_R
+    localtime_r(&now, &ltime);
+#else
+    ltime = *localtime(&now);
+#endif
+    strftime(buf, sizeof(buf), arg, &ltime);
+    INSERT_STR(buf);
+    return dst;
+}
+
+static char *insert_color(CODES_MAP_ARGS)
+{
+    INSERT_STR("");
+    *in += 1; // skip id
+    return dst;
+}
+
+static char *insert_font(CODES_MAP_ARGS)
+{
+    INSERT_STR("");
+    *in += 1; // skip id
+    return dst;
+}
+
+static const struct {
+    const char *from;
+    const char *arg;
+    char *(*func)(CODES_MAP_ARGS);
+} ass_codes_map[] = {
+    {"\\~", "~",        insert_text},       // tilde doesn't need escaping
+    {"~",   "{\\h}",    insert_text},       // hard space
+    {"\\n", "\\N",      insert_text},       // newline
+    {"\\D", "%d %b %Y", insert_datetime},   // current date
+    {"\\T", "%H:%M",    insert_datetime},   // current time
+    {"\\N", "{\\r}",    insert_text},       // reset to default style
+    {"\\I", "{\\i1}",   insert_text},       // italic on
+    {"\\i", "{\\i0}",   insert_text},       // italic off
+    {"\\B", "{\\b1}",   insert_text},       // bold on
+    {"\\b", "{\\b0}",   insert_text},       // bold off
+    {"\\U", "{\\u1}",   insert_text},       // underline on
+    {"\\u", "{\\u0}",   insert_text},       // underline off
+    {"\\C", "",         insert_color},      // TODO: color
+    {"\\F", "",         insert_font},       // TODO: font
+};
+
+enum {
+    ALIGN_VB = 1<<0, // vertical bottom, default
+    ALIGN_VM = 1<<1, // vertical middle
+    ALIGN_VT = 1<<2, // vertical top
+    ALIGN_JC = 1<<3, // justify center, default
+    ALIGN_JL = 1<<4, // justify left
+    ALIGN_JR = 1<<5, // justify right
+};
+
+static void jacosub_to_ass(AVCodecContext *avctx, char *dst, char *dst_end,
+                           const char *src)
+{
+    int i, valign = 0, halign = 0;
+    char c = av_toupper(*src);
+    char directives[128] = {0};
+
+    /* extract the optional directives */
+    if ((c >= 'A' && c <= 'Z') || c == '[') {
+        char *p    = directives;
+        char *pend = directives + sizeof(directives) - 1;
+
+        do *p++ = av_toupper(*src++);
+        while (*src && !jss_whitespace(*src) && p < pend);
+        *p = 0;
+        src = jss_skip_whitespace(src);
+    }
+
+    /* handle directives (TODO: handle more of them, and more reliably) */
+    if      (strstr(directives, "VB")) valign = ALIGN_VB;
+    else if (strstr(directives, "VM")) valign = ALIGN_VM;
+    else if (strstr(directives, "VT")) valign = ALIGN_VT;
+    if      (strstr(directives, "JC")) halign = ALIGN_JC;
+    else if (strstr(directives, "JL")) halign = ALIGN_JL;
+    else if (strstr(directives, "JR")) halign = ALIGN_JR;
+    if (valign || halign) {
+        if (!valign) valign = ALIGN_VB;
+        if (!halign) halign = ALIGN_JC;
+        switch (valign | halign) {
+        case ALIGN_VB | ALIGN_JL: INSERT_STR("{\\an1}"); break; // bottom left
+        case ALIGN_VB | ALIGN_JC: INSERT_STR("{\\an2}"); break; // bottom center
+        case ALIGN_VB | ALIGN_JR: INSERT_STR("{\\an3}"); break; // bottom right
+        case ALIGN_VM | ALIGN_JL: INSERT_STR("{\\an4}"); break; // middle left
+        case ALIGN_VM | ALIGN_JC: INSERT_STR("{\\an5}"); break; // middle center
+        case ALIGN_VM | ALIGN_JR: INSERT_STR("{\\an6}"); break; // middle right
+        case ALIGN_VT | ALIGN_JL: INSERT_STR("{\\an7}"); break; // top left
+        case ALIGN_VT | ALIGN_JC: INSERT_STR("{\\an8}"); break; // top center
+        case ALIGN_VT | ALIGN_JR: INSERT_STR("{\\an9}"); break; // top right
+        }
+    }
+
+    /* process timed line */
+    while (dst < dst_end && *src && *src != '\n') {
+
+        /* text continue on the next line */
+        if (src[0] == '\\' && src[1] == '\n') {
+            src += 2;
+            while (jss_whitespace(*src))
+                src++;
+            continue;
+        }
+
+        /* special character codes */
+        for (i = 0; i < FF_ARRAY_ELEMS(ass_codes_map); i++) {
+            const char *from = ass_codes_map[i].from;
+            const char *arg  = ass_codes_map[i].arg;
+            size_t codemap_len = strlen(from);
+
+            if (!strncmp(src, from, codemap_len)) {
+                src += codemap_len;
+                dst = ass_codes_map[i].func(dst, dst_end, &src, arg);
+                break;
+            }
+        }
+
+        /* simple char copy */
+        if (i == FF_ARRAY_ELEMS(ass_codes_map))
+            *dst++ = *src++;
+    }
+    INSERT_STR("\r\n");
+}
+
+static const char *read_ts(JACOsubContext *jacosub, const char *buf,
+                           int *ts_start, int *ts_end)
+{
+    int len;
+    unsigned hs, ms, ss, fs; // hours, minutes, seconds, frame start
+    unsigned he, me, se, fe; // hours, minutes, seconds, frame end
+
+    /* timed format */
+    if (sscanf(buf, "%u:%u:%u.%u %u:%u:%u.%u %n",
+               &hs, &ms, &ss, &fs,
+               &he, &me, &se, &fe, &len) == 8) {
+        *ts_start = (hs*3600 + ms*60 + ss) * jacosub->timeres + fs;
+        *ts_end   = (he*3600 + me*60 + se) * jacosub->timeres + fe;
+        goto shift_and_ret;
+    }
+
+    /* timestamps format */
+    if (sscanf(buf, "@%u @%u %n", ts_start, ts_end, &len) == 2)
+        goto shift_and_ret;
+
+    return NULL;
+
+shift_and_ret:
+    *ts_start = (*ts_start + jacosub->shift) * 100 / jacosub->timeres;
+    *ts_end   = (*ts_end   + jacosub->shift) * 100 / jacosub->timeres;
+    return buf + len;
+}
+
+static int jacosub_decode_frame(AVCodecContext *avctx,
+                                void *data, int *got_sub_ptr, AVPacket *avpkt)
+{
+    AVSubtitle *sub = data;
+    JACOsubContext *jacosub = avctx->priv_data;
+    int ts_start, ts_end;
+    char buffer[JSS_MAX_LINESIZE];
+    const char *ptr = avpkt->data;
+
+    if (avpkt->size <= 0)
+        goto end;
+
+    ptr = read_ts(jacosub, ptr, &ts_start, &ts_end);
+    if (!ptr)
+        goto end;
+
+    jacosub_to_ass(avctx, buffer, buffer + sizeof(buffer), ptr);
+    ff_ass_add_rect(sub, buffer, ts_start, ts_end, 0);
+
+end:
+    *got_sub_ptr = sub->num_rects > 0;
+    return avpkt->size;
+}
+
+static int jacosub_init(AVCodecContext *avctx)
+{
+    JACOsubContext *jacosub = avctx->priv_data;
+    const char *header      = avctx->extradata;
+    const char *header_end  = header + avctx->extradata_size;
+
+    jacosub->timeres = 30;
+    while (header && header < header_end && *header) {
+        switch (header[1]) {
+        case 'S':
+            jacosub->shift = jss_get_shift(jacosub->timeres, header + 3);
+            break;
+        case 'T':
+            jacosub->timeres = strtol(header + 3, NULL, 10);
+            break;
+        }
+        header += strcspn(header, "\n") + 1;
+    }
+    return ff_ass_subtitle_header_default(avctx);
+}
+
+AVCodec ff_jacosub_decoder = {
+    .name           = "jacosub",
+    .long_name      = NULL_IF_CONFIG_SMALL("JACOsub subtitle"),
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = CODEC_ID_JACOSUB,
+    .init           = jacosub_init,
+    .decode         = jacosub_decode_frame,
+    .priv_data_size = sizeof(JACOsubContext),
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index ac28707..db72466 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -21,8 +21,8 @@
 #define AVCODEC_VERSION_H
 
 #define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR  0
-#define LIBAVCODEC_VERSION_MICRO 102
+#define LIBAVCODEC_VERSION_MINOR  1
+#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 bd2b17b..a307d6c 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -128,6 +128,8 @@ OBJS-$(CONFIG_ISS_DEMUXER)               += iss.o
 OBJS-$(CONFIG_IV8_DEMUXER)               += iv8.o
 OBJS-$(CONFIG_IVF_DEMUXER)               += ivfdec.o
 OBJS-$(CONFIG_IVF_MUXER)                 += ivfenc.o
+OBJS-$(CONFIG_JACOSUB_DEMUXER)           += jacosubdec.o
+OBJS-$(CONFIG_JACOSUB_MUXER)             += rawenc.o
 OBJS-$(CONFIG_JV_DEMUXER)                += jvdec.o
 OBJS-$(CONFIG_LATM_DEMUXER)              += rawdec.o
 OBJS-$(CONFIG_LATM_MUXER)                += latmenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 134839f..d4ceead 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -123,6 +123,7 @@ void av_register_all(void)
     REGISTER_DEMUXER  (ISS, iss);
     REGISTER_DEMUXER  (IV8, iv8);
     REGISTER_MUXDEMUX (IVF, ivf);
+    REGISTER_MUXDEMUX (JACOSUB, jacosub);
     REGISTER_DEMUXER  (JV, jv);
     REGISTER_MUXDEMUX (LATM, latm);
     REGISTER_DEMUXER  (LMLM4, lmlm4);
diff --git a/libavformat/jacosubdec.c b/libavformat/jacosubdec.c
new file mode 100644
index 0000000..e5e825f
--- /dev/null
+++ b/libavformat/jacosubdec.c
@@ -0,0 +1,220 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * Copyright (c) 2012 Clément Bœsch
+ *
+ * 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
+ * JACOsub subtitle demuxer
+ * @see http://unicorn.us.com/jacosub/jscripts.html
+ * @todo Support P[ALETTE] directive.
+ * @todo Store all the events in the read header callback and raise them just
+ *       like ASS demuxer.
+ */
+
+#include "avformat.h"
+#include "internal.h"
+#include "libavcodec/jacosub.h"
+#include "libavutil/avstring.h"
+#include "libavutil/intreadwrite.h"
+
+typedef struct {
+    int shift;
+    unsigned timeres;
+} JACOsubContext;
+
+static int jacosub_probe(AVProbeData *p)
+{
+    unsigned char c;
+    const char *ptr     = p->buf;
+    const char *ptr_end = p->buf + p->buf_size;
+
+    if (AV_RB24(ptr) == 0xEFBBBF)
+        ptr += 3; /* skip UTF-8 BOM */
+
+    while (ptr < ptr_end) {
+        if (sscanf(ptr, "%*u:%*u:%*u.%*u %*u:%*u:%*u.%*u %c", &c) == 1 ||
+            sscanf(ptr, "@%*u @%*u %c",                       &c) == 1)
+            return AVPROBE_SCORE_MAX;
+        while (jss_whitespace(*ptr))
+            ptr++;
+        if (*ptr && *ptr == '#')
+            return AVPROBE_SCORE_MAX/3;
+        ptr += strcspn(ptr, "\n") + 1;
+    }
+    return 0;
+}
+
+static const char * const cmds[] = {
+    "CLOCKPAUSE",
+    "DIRECTIVE",
+    "FONT",
+    "HRES",
+    "INCLUDE",
+    "PALETTE",
+    "QUANTIZE",
+    "RAMP",
+    "SHIFT",
+    "TIMERES",
+};
+
+static int get_jss_cmd(char k)
+{
+    int i;
+
+    k = av_toupper(k);
+    for (i = 0; i < FF_ARRAY_ELEMS(cmds); i++)
+        if (k == cmds[i][0])
+            return i;
+    return -1;
+}
+
+#define MAX_HEADER_SIZE 1024 /* more than enough to store two directives */
+#define APPEND_HEADER do {                               \
+    hdr_pos += snprintf(st->codec->extradata + hdr_pos,  \
+                        MAX_HEADER_SIZE,                 \
+                        "#%c %s\n", cmds[i][0], p);      \
+} while (0)
+
+static int jacosub_read_header(AVFormatContext *s)
+{
+    AVIOContext *pb = s->pb;
+    char line[JSS_MAX_LINESIZE];
+    JACOsubContext *jacosub = s->priv_data;
+    int shift_set = 0; // only the first shift matters
+    AVStream *st;
+    int64_t pos = avio_tell(s->pb);
+    int hdr_pos = 0;
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    avpriv_set_pts_info(st, 64, 1, 100);
+    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+    st->codec->codec_id   = CODEC_ID_JACOSUB;
+    st->codec->extradata  = av_mallocz(MAX_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
+
+    jacosub->timeres = 30;
+
+    while (!url_feof(pb)) {
+        int i, cmd_len;
+        const char *p = line;
+
+        ff_get_line(pb, line, sizeof(line));
+
+        /* skip all non-compiler commands and focus on the command */
+        p = jss_skip_whitespace(p);
+        if (*p != '#')
+            continue;
+        p++;
+        i = get_jss_cmd(p[0]);
+        if (i == -1)
+            continue;
+
+        /* trim command + spaces */
+        cmd_len = strlen(cmds[i]);
+        if (av_strncasecmp(p, cmds[i], cmd_len) == 0)
+            p += cmd_len;
+        else
+            p++;
+        p = jss_skip_whitespace(p);
+
+        /* save commands which affect the whole script */
+        switch (cmds[i][0]) {
+        case 'S': // SHIFT command affect the whole script...
+            if (!shift_set) {
+                jacosub->shift = jss_get_shift(jacosub->timeres, p);
+                APPEND_HEADER;
+                shift_set = 1;
+            }
+            break;
+        case 'T': // ...but must be placed after TIMERES
+            jacosub->timeres = strtol(p, NULL, 10);
+            APPEND_HEADER;
+            break;
+        }
+    }
+    st->codec->extradata_size = hdr_pos;
+
+    return avio_seek(pb, pos, SEEK_SET);
+}
+
+static int64_t get_pts(JACOsubContext *ctx, const char *buf)
+{
+    unsigned ts, hh, mm, ss, ff;
+    char c;
+
+    /* timed format */
+    if (sscanf(buf, "%u:%u:%u.%u %*u:%*u:%*u.%*u %c",
+               &hh, &mm, &ss, &ff, &c) == 5) {
+        ts = (hh*3600 + mm*60 + ss) * ctx->timeres + ff;
+        return (ts + ctx->shift) * 100 / ctx->timeres;
+    }
+
+    /* timestamps format */
+    if (sscanf(buf, "@%u @%*u %c", &ts, &c) == 2)
+        return (ts + ctx->shift) * 100 / ctx->timeres;
+
+    return AV_NOPTS_VALUE;
+}
+
+static int jacosub_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVIOContext *pb = s->pb;
+    char line[JSS_MAX_LINESIZE];
+    int len, res;
+    int64_t pos = avio_tell(s->pb);
+    JACOsubContext *jacosub = s->priv_data;
+
+    /* get a directive or timed line */
+    len = ff_get_line(pb, line, sizeof(line));
+    if (!*line)
+        return AVERROR_EOF;
+
+    /* new packet with the main line and settings */
+    res = av_new_packet(pkt, len);
+    if (res)
+        return res;
+    memcpy(pkt->data, line, len);
+    pkt->flags |= AV_PKT_FLAG_KEY;
+    pkt->pos = pos;
+    pkt->pts = pkt->dts = get_pts(jacosub, line);
+
+    /* extend packet if line ends up with a '\' */
+    while (len > 1 && !strcmp(&line[len - 2], "\\\n")) {
+        int old_size = pkt->size;
+        len = ff_get_line(pb, line, sizeof(line));
+        if (!*line)
+            break;
+        res = av_grow_packet(pkt, len);
+        if (res)
+            return res;
+        memcpy(pkt->data + old_size, line, len);
+    }
+    return 0;
+}
+
+AVInputFormat ff_jacosub_demuxer = {
+    .name           = "jacosub",
+    .long_name      = NULL_IF_CONFIG_SMALL("JACOsub subtitle format"),
+    .priv_data_size = sizeof(JACOsubContext),
+    .read_probe     = jacosub_probe,
+    .read_header    = jacosub_read_header,
+    .read_packet    = jacosub_read_packet,
+    .flags          = AVFMT_GENERIC_INDEX,
+};
diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c
index 4c1ede4..c57acbd 100644
--- a/libavformat/rawenc.c
+++ b/libavformat/rawenc.c
@@ -183,6 +183,18 @@ AVOutputFormat ff_cavsvideo_muxer = {
 };
 #endif
 
+#if CONFIG_JACOSUB_MUXER
+AVOutputFormat ff_jacosub_muxer = {
+    .name           = "jacosub",
+    .long_name      = NULL_IF_CONFIG_SMALL("JACOsub subtitle format"),
+    .mime_type      = "text/x-jacosub",
+    .extensions     = "jss,js",
+    .write_packet   = ff_raw_write_packet,
+    .flags          = AVFMT_NOTIMESTAMPS,
+    .subtitle_codec = CODEC_ID_JACOSUB,
+};
+#endif
+
 #if CONFIG_M4V_MUXER
 AVOutputFormat ff_m4v_muxer = {
     .name              = "m4v",
diff --git a/libavformat/version.h b/libavformat/version.h
index 8a86c10..ea53818 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  0
+#define LIBAVFORMAT_VERSION_MINOR  1
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
1.7.9



More information about the ffmpeg-devel mailing list