[FFmpeg-cvslog] JACOsub demuxer, decoder and muxer.

Clément Bœsch git at videolan.org
Thu May 3 19:40:42 CEST 2012


ffmpeg | branch: master | Clément Bœsch <ubitux at gmail.com> | Mon Apr 18 17:40:51 2011 +0200| [9ae570fb5fc7a0401a7cb37bac82086a07aed8a5] | committer: Clément Bœsch

JACOsub demuxer, decoder and muxer.

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

 MAINTAINERS              |    2 +
 doc/general.texi         |    1 +
 libavcodec/Makefile      |    1 +
 libavcodec/allcodecs.c   |    1 +
 libavcodec/avcodec.h     |    1 +
 libavcodec/jacosub.h     |   43 +++++++
 libavcodec/jacosubdec.c  |  207 +++++++++++++++++++++++++++++++
 libavcodec/version.h     |    2 +-
 libavformat/Makefile     |    2 +
 libavformat/allformats.c |    1 +
 libavformat/jacosubdec.c |  304 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/jacosubenc.c |   42 +++++++
 libavformat/version.h    |    2 +-
 13 files changed, 607 insertions(+), 2 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index f1a46b7..25c3f5c 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -159,6 +159,7 @@ Codecs:
   indeo5*                               Kostya Shishkov
   interplayvideo.c                      Mike Melanson
   ivi*                                  Kostya Shishkov
+  jacosub*                              Clément Bœsch
   jpeg_ls.c                             Kostya Shishkov
   jvdec.c                               Peter Ross
   kmvc.c                                Kostya Shishkov
@@ -315,6 +316,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 a999acf..2fbcbbf 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -837,6 +837,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 X
 @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 28d4a1c..2a1b15f 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -221,6 +221,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 d31d29a..c12c783 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -397,6 +397,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC  (ASS, ass);
     REGISTER_ENCDEC  (DVBSUB, dvbsub);
     REGISTER_ENCDEC  (DVDSUB, dvdsub);
+    REGISTER_DECODER (JACOSUB, jacosub);
     REGISTER_DECODER (MICRODVD, microdvd);
     REGISTER_DECODER (PGSSUB, pgssub);
     REGISTER_ENCDEC  (SRT, srt);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 2e56a02..b1c6d47 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -423,6 +423,7 @@ enum CodecID {
     CODEC_ID_SRT,
     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'),
 
     /* 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..410969e
--- /dev/null
+++ b/libavcodec/jacosub.h
@@ -0,0 +1,43 @@
+/*
+ * 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;
+}
+
+#endif /* AVCODEC_JACOSUB_H */
diff --git a/libavcodec/jacosubdec.c b/libavcodec/jacosubdec.c
new file mode 100644
index 0000000..8c02d3b
--- /dev/null
+++ b/libavcodec/jacosubdec.c
@@ -0,0 +1,207 @@
+/*
+ * 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"
+#include "libavutil/bprint.h"
+
+#undef time
+
+static int insert_text(AVBPrint *dst, const char *in, const char *arg)
+{
+    av_bprintf(dst, "%s", arg);
+    return 0;
+}
+
+static int insert_datetime(AVBPrint *dst, const char *in, const char *arg)
+{
+    char buf[16] = {0};
+    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);
+    av_bprintf(dst, "%s", buf);
+    return 0;
+}
+
+static int insert_color(AVBPrint *dst, const char *in, const char *arg)
+{
+    return 1; // skip id
+}
+
+static int insert_font(AVBPrint *dst, const char *in, const char *arg)
+{
+    return 1; // skip id
+}
+
+static const struct {
+    const char *from;
+    const char *arg;
+    int (*func)(AVBPrint *dst, const char *in, const char *arg);
+} 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, AVBPrint *dst, 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: av_bprintf(dst, "{\\an1}"); break; // bottom left
+        case ALIGN_VB | ALIGN_JC: av_bprintf(dst, "{\\an2}"); break; // bottom center
+        case ALIGN_VB | ALIGN_JR: av_bprintf(dst, "{\\an3}"); break; // bottom right
+        case ALIGN_VM | ALIGN_JL: av_bprintf(dst, "{\\an4}"); break; // middle left
+        case ALIGN_VM | ALIGN_JC: av_bprintf(dst, "{\\an5}"); break; // middle center
+        case ALIGN_VM | ALIGN_JR: av_bprintf(dst, "{\\an6}"); break; // middle right
+        case ALIGN_VT | ALIGN_JL: av_bprintf(dst, "{\\an7}"); break; // top left
+        case ALIGN_VT | ALIGN_JC: av_bprintf(dst, "{\\an8}"); break; // top center
+        case ALIGN_VT | ALIGN_JR: av_bprintf(dst, "{\\an9}"); break; // top right
+        }
+    }
+
+    /* process timed line */
+    while (*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;
+                src += ass_codes_map[i].func(dst, src, arg);
+                break;
+            }
+        }
+
+        /* simple char copy */
+        if (i == FF_ARRAY_ELEMS(ass_codes_map))
+            av_bprintf(dst, "%c", *src++);
+    }
+    av_bprintf(dst, "\r\n");
+}
+
+static int jacosub_decode_frame(AVCodecContext *avctx,
+                                void *data, int *got_sub_ptr, AVPacket *avpkt)
+{
+    AVSubtitle *sub = data;
+    const char *ptr = avpkt->data;
+
+    if (avpkt->size <= 0)
+        goto end;
+
+    if (*ptr) {
+        int ts_start = avpkt->pts;
+        int ts_end   = avpkt->pts + avpkt->duration;
+        AVBPrint buffer;
+        char *dec_sub;
+
+        // skip timers
+        ptr = jss_skip_whitespace(ptr);
+        ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
+        ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
+
+        av_bprint_init(&buffer, JSS_MAX_LINESIZE, JSS_MAX_LINESIZE);
+        jacosub_to_ass(avctx, &buffer, ptr);
+        av_bprint_finalize(&buffer, &dec_sub);
+        ff_ass_add_rect(sub, dec_sub, ts_start, ts_end, 0);
+        av_free(dec_sub);
+    }
+
+end:
+    *got_sub_ptr = sub->num_rects > 0;
+    return avpkt->size;
+}
+
+AVCodec ff_jacosub_decoder = {
+    .name           = "jacosub",
+    .long_name      = NULL_IF_CONFIG_SMALL("JACOsub subtitle"),
+    .type           = AVMEDIA_TYPE_SUBTITLE,
+    .id             = CODEC_ID_JACOSUB,
+    .init           = ff_ass_subtitle_header_default,
+    .decode         = jacosub_decode_frame,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 3852e6b..1c142d4 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -27,7 +27,7 @@
  */
 
 #define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR  18
+#define LIBAVCODEC_VERSION_MINOR  19
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 212c9d7..c034296 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -131,6 +131,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)             += jacosubenc.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 905a165..ad2693a 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -125,6 +125,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..30c4f63
--- /dev/null
+++ b/libavformat/jacosubdec.c
@@ -0,0 +1,304 @@
+/*
+ * 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 demuxer
+ * @see http://unicorn.us.com/jacosub/jscripts.html
+ * @todo Support P[ALETTE] directive.
+ */
+
+#include "avformat.h"
+#include "internal.h"
+#include "libavcodec/jacosub.h"
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
+#include "libavutil/intreadwrite.h"
+
+typedef struct {
+    char *line;         ///< null-terminated heap allocated subtitle line
+    int64_t pos;        ///< offset position
+    int start;          ///< timestamp start
+    int end;            ///< timestamp end
+} SubEntry;
+
+typedef struct {
+    int shift;
+    unsigned timeres;
+    SubEntry *subs;     ///< subtitles list
+    int nsub;           ///< number of subtitles
+    int sid;            ///< current subtitle
+} JACOsubContext;
+
+static int timed_line(const char *ptr)
+{
+    char c;
+    return (sscanf(ptr, "%*u:%*u:%*u.%*u %*u:%*u:%*u.%*u %c", &c) == 1 ||
+            sscanf(ptr, "@%*u @%*u %c",                       &c) == 1);
+}
+
+static int jacosub_probe(AVProbeData *p)
+{
+    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 (timed_line(ptr))
+            return AVPROBE_SCORE_MAX / 2;
+        while (jss_whitespace(*ptr))
+            ptr++;
+        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;
+}
+
+static int jacosub_read_close(AVFormatContext *s)
+{
+    int i;
+    JACOsubContext *jacosub = s->priv_data;
+
+    for (i = 0; i < jacosub->nsub; i++)
+        av_freep(&jacosub->subs[i].line);
+    jacosub->nsub = 0;
+    av_freep(&jacosub->subs);
+    return 0;
+}
+
+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 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;
+}
+
+static int cmp_timed_sub(const void *a, const void *b)
+{
+    return ((const SubEntry*)a)->start - ((const SubEntry*)b)->start;
+}
+
+static int jacosub_read_header(AVFormatContext *s)
+{
+    AVBPrint header;
+    AVIOContext *pb = s->pb;
+    char line[JSS_MAX_LINESIZE];
+    JACOsubContext *jacosub = s->priv_data;
+    int shift_set = 0; // only the first shift matters
+    int merge_line = 0;
+    int i;
+
+    AVStream *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;
+
+    jacosub->timeres = 30;
+
+    av_bprint_init(&header, 1024+FF_INPUT_BUFFER_PADDING_SIZE, 4096);
+
+    while (!url_feof(pb)) {
+        int cmd_len;
+        const char *p = line;
+        int64_t pos = avio_tell(pb);
+
+        ff_get_line(pb, line, sizeof(line));
+
+        p = jss_skip_whitespace(p);
+
+        /* queue timed line */
+        if (merge_line || timed_line(p)) {
+            SubEntry *subs, *sub;
+            const int len = strlen(line);
+
+            if (merge_line) {
+                char *tmp;
+                const int old_len = strlen(sub->line);
+
+                sub = &subs[jacosub->nsub];
+                tmp = av_realloc(sub->line, old_len + len + 1);
+                if (!tmp)
+                    return AVERROR(ENOMEM);
+                sub->line = tmp;
+                strcpy(sub->line + old_len, line);
+            } else {
+                subs = av_realloc(jacosub->subs,
+                                  sizeof(*jacosub->subs) * (jacosub->nsub+1));
+                if (!subs)
+                    return AVERROR(ENOMEM);
+                jacosub->subs = subs;
+                sub = &subs[jacosub->nsub];
+                sub->pos  = pos;
+                sub->line = av_strdup(line);
+                if (!sub->line)
+                    return AVERROR(ENOMEM);
+            }
+            merge_line = len > 1 && !strcmp(&line[len - 2], "\\\n");
+            if (!merge_line)
+                jacosub->nsub++;
+            continue;
+        }
+
+        /* skip all non-compiler commands and focus on the command */
+        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);
+
+        /* handle commands which affect the whole script */
+        switch (cmds[i][0]) {
+        case 'S': // SHIFT command affect the whole script...
+            if (!shift_set) {
+                jacosub->shift = get_shift(jacosub->timeres, p);
+                shift_set = 1;
+            }
+            av_bprintf(&header, "#S %s", p);
+            break;
+        case 'T': // ...but must be placed after TIMERES
+            jacosub->timeres = strtol(p, NULL, 10);
+            av_bprintf(&header, "#T %s", p);
+            break;
+        }
+    }
+
+    /* general/essential directives in the extradata */
+    av_bprint_finalize(&header, (char **)&st->codec->extradata);
+    st->codec->extradata_size = header.len + 1;
+
+    /* SHIFT and TIMERES affect the whole script so packet timing can only be
+     * done in a second pass */
+    for (i = 0; i < jacosub->nsub; i++) {
+        SubEntry *sub = &jacosub->subs[i];
+        read_ts(jacosub, sub->line, &sub->start, &sub->end);
+    }
+    qsort(jacosub->subs, jacosub->nsub, sizeof(*jacosub->subs), cmp_timed_sub);
+
+    return 0;
+}
+
+static int jacosub_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int res;
+    JACOsubContext *jacosub = s->priv_data;
+    const SubEntry *sub = &jacosub->subs[jacosub->sid++];
+
+    if (jacosub->sid == jacosub->nsub)
+        return AVERROR_EOF;
+    res = av_new_packet(pkt, strlen(sub->line));
+    if (res)
+        return res;
+    strcpy(pkt->data, sub->line);
+    pkt->flags |= AV_PKT_FLAG_KEY;
+    pkt->pos = sub->pos;
+    pkt->pts = pkt->dts = sub->start;
+    pkt->duration = sub->end - sub->start;
+    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,
+    .read_close     = jacosub_read_close,
+    .flags          = AVFMT_GENERIC_INDEX,
+};
diff --git a/libavformat/jacosubenc.c b/libavformat/jacosubenc.c
new file mode 100644
index 0000000..57866b3
--- /dev/null
+++ b/libavformat/jacosubenc.c
@@ -0,0 +1,42 @@
+/*
+ * 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 "rawenc.h"
+
+static int jacosub_write_header(AVFormatContext *s)
+{
+    const AVCodecContext *avctx = s->streams[0]->codec;
+
+    if (avctx->extradata_size) {
+        avio_write(s->pb, avctx->extradata, avctx->extradata_size - 1);
+        avio_flush(s->pb);
+    }
+    return 0;
+}
+
+AVOutputFormat ff_jacosub_muxer = {
+    .name           = "jacosub",
+    .long_name      = NULL_IF_CONFIG_SMALL("JACOsub subtitle format"),
+    .mime_type      = "text/x-jacosub",
+    .extensions     = "jss,js",
+    .write_header   = jacosub_write_header,
+    .write_packet   = ff_raw_write_packet,
+    .flags          = AVFMT_TS_NONSTRICT,
+    .subtitle_codec = CODEC_ID_JACOSUB,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 7c4d8e7..a3143c3 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  3
+#define LIBAVFORMAT_VERSION_MINOR  4
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list