aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat
diff options
context:
space:
mode:
authorClément Bœsch <ubitux@gmail.com>2011-04-18 17:40:51 +0200
committerClément Bœsch <ubitux@gmail.com>2012-05-03 19:37:14 +0200
commit9ae570fb5fc7a0401a7cb37bac82086a07aed8a5 (patch)
treea27321dec782d95a4185c88f421631dccbc785e6 /libavformat
parent234e00259ba036b53dc24f78f23c6afe8f108d74 (diff)
downloadffmpeg-9ae570fb5fc7a0401a7cb37bac82086a07aed8a5.tar.gz
JACOsub demuxer, decoder and muxer.
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/Makefile2
-rw-r--r--libavformat/allformats.c1
-rw-r--r--libavformat/jacosubdec.c304
-rw-r--r--libavformat/jacosubenc.c42
-rw-r--r--libavformat/version.h2
5 files changed, 350 insertions, 1 deletions
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 212c9d7b50..c034296a95 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 905a1653f6..ad2693ad12 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 0000000000..30c4f636bd
--- /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 0000000000..57866b3995
--- /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 7c4d8e70b9..a3143c395f 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, \