2011-04-18 15:40:51 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2012-06-15 16:57:39 +00:00
|
|
|
#include "subtitles.h"
|
2012-12-29 21:09:59 +00:00
|
|
|
#include "libavcodec/internal.h"
|
2011-04-18 15:40:51 +00:00
|
|
|
#include "libavcodec/jacosub.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/bprint.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int shift;
|
|
|
|
unsigned timeres;
|
2012-06-15 16:57:39 +00:00
|
|
|
FFDemuxSubtitlesQueue q;
|
2011-04-18 15:40:51 +00:00
|
|
|
} JACOsubContext;
|
|
|
|
|
|
|
|
static int timed_line(const char *ptr)
|
|
|
|
{
|
|
|
|
char c;
|
2013-11-19 11:53:14 +00:00
|
|
|
int fs, fe;
|
2011-04-18 15:40:51 +00:00
|
|
|
return (sscanf(ptr, "%*u:%*u:%*u.%*u %*u:%*u:%*u.%*u %c", &c) == 1 ||
|
2013-11-19 11:53:14 +00:00
|
|
|
(sscanf(ptr, "@%u @%u %c", &fs, &fe, &c) == 3 && fs < fe));
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int jacosub_probe(const AVProbeData *p)
|
2011-04-18 15:40:51 +00:00
|
|
|
{
|
|
|
|
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) {
|
|
|
|
while (jss_whitespace(*ptr))
|
|
|
|
ptr++;
|
2012-05-31 19:12:25 +00:00
|
|
|
if (*ptr != '#' && *ptr != '\n') {
|
|
|
|
if (timed_line(ptr))
|
2013-05-05 11:16:53 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION + 1;
|
2012-05-31 19:12:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-09-08 16:02:45 +00:00
|
|
|
ptr += ff_subtitles_next_line(ptr);
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
JACOsubContext *jacosub = s->priv_data;
|
2012-06-15 16:57:39 +00:00
|
|
|
ff_subtitles_queue_clean(&jacosub->q);
|
2011-04-18 15:40:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *read_ts(JACOsubContext *jacosub, const char *buf,
|
2015-09-29 13:14:59 +00:00
|
|
|
int64_t *start, int64_t *duration)
|
2011-04-18 15:40:51 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
unsigned hs, ms, ss, fs; // hours, minutes, seconds, frame start
|
|
|
|
unsigned he, me, se, fe; // hours, minutes, seconds, frame end
|
2012-06-15 16:57:39 +00:00
|
|
|
int ts_start, ts_end;
|
2019-06-07 20:43:42 +00:00
|
|
|
int64_t ts_start64, ts_end64;
|
2011-04-18 15:40:51 +00:00
|
|
|
|
|
|
|
/* timed format */
|
|
|
|
if (sscanf(buf, "%u:%u:%u.%u %u:%u:%u.%u %n",
|
|
|
|
&hs, &ms, &ss, &fs,
|
|
|
|
&he, &me, &se, &fe, &len) == 8) {
|
2012-06-15 16:57:39 +00:00
|
|
|
ts_start = (hs*3600 + ms*60 + ss) * jacosub->timeres + fs;
|
|
|
|
ts_end = (he*3600 + me*60 + se) * jacosub->timeres + fe;
|
2011-04-18 15:40:51 +00:00
|
|
|
goto shift_and_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timestamps format */
|
2012-06-15 16:57:39 +00:00
|
|
|
if (sscanf(buf, "@%u @%u %n", &ts_start, &ts_end, &len) == 2)
|
2011-04-18 15:40:51 +00:00
|
|
|
goto shift_and_ret;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
shift_and_ret:
|
2021-02-11 21:44:36 +00:00
|
|
|
ts_start64 = (ts_start + (int64_t)jacosub->shift) * 100LL / jacosub->timeres;
|
|
|
|
ts_end64 = (ts_end + (int64_t)jacosub->shift) * 100LL / jacosub->timeres;
|
2019-06-07 20:43:42 +00:00
|
|
|
*start = ts_start64;
|
|
|
|
*duration = ts_end64 - ts_start64;
|
2011-04-18 15:40:51 +00:00
|
|
|
return buf + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_shift(int timeres, const char *buf)
|
|
|
|
{
|
|
|
|
int sign = 1;
|
|
|
|
int a = 0, b = 0, c = 0, d = 0;
|
2020-10-19 15:29:01 +00:00
|
|
|
int64_t ret;
|
2011-04-18 15:40:51 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:29:01 +00:00
|
|
|
ret = 0;
|
2011-04-18 15:40:51 +00:00
|
|
|
switch (n) {
|
2020-10-29 12:36:22 +00:00
|
|
|
case 4:
|
|
|
|
ret = sign * (((int64_t)a*3600 + b*60 + c) * timeres + d);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ret = sign * (( (int64_t)a*60 + b) * timeres + c);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ret = sign * (( (int64_t)a) * timeres + b);
|
|
|
|
break;
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
2020-10-19 15:29:01 +00:00
|
|
|
if ((int)ret != ret)
|
|
|
|
ret = 0;
|
2011-04-18 15:40:51 +00:00
|
|
|
|
2020-10-19 15:29:01 +00:00
|
|
|
return ret;
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-12-29 21:09:59 +00:00
|
|
|
int i, ret;
|
2011-04-18 15:40:51 +00:00
|
|
|
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
avpriv_set_pts_info(st, 64, 1, 100);
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_JACOSUB;
|
2011-04-18 15:40:51 +00:00
|
|
|
|
|
|
|
jacosub->timeres = 30;
|
|
|
|
|
2015-07-27 20:53:16 +00:00
|
|
|
av_bprint_init(&header, 1024+AV_INPUT_BUFFER_PADDING_SIZE, 4096);
|
2011-04-18 15:40:51 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
while (!avio_feof(pb)) {
|
2011-04-18 15:40:51 +00:00
|
|
|
int cmd_len;
|
|
|
|
const char *p = line;
|
|
|
|
int64_t pos = avio_tell(pb);
|
2012-06-15 16:57:39 +00:00
|
|
|
int len = ff_get_line(pb, line, sizeof(line));
|
2011-04-18 15:40:51 +00:00
|
|
|
|
|
|
|
p = jss_skip_whitespace(p);
|
|
|
|
|
|
|
|
/* queue timed line */
|
|
|
|
if (merge_line || timed_line(p)) {
|
2012-06-15 16:57:39 +00:00
|
|
|
AVPacket *sub;
|
|
|
|
|
|
|
|
sub = ff_subtitles_queue_insert(&jacosub->q, line, len, merge_line);
|
2020-06-14 01:09:08 +00:00
|
|
|
if (!sub) {
|
2021-03-24 02:15:33 +00:00
|
|
|
av_bprint_finalize(&header, NULL);
|
2020-06-14 01:09:08 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-06-15 16:57:39 +00:00
|
|
|
sub->pos = pos;
|
2011-04-18 15:40:51 +00:00
|
|
|
merge_line = len > 1 && !strcmp(&line[len - 2], "\\\n");
|
|
|
|
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);
|
2012-06-29 17:38:09 +00:00
|
|
|
if (!jacosub->timeres)
|
|
|
|
jacosub->timeres = 30;
|
|
|
|
else
|
|
|
|
av_bprintf(&header, "#T %s", p);
|
2011-04-18 15:40:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* general/essential directives in the extradata */
|
2016-04-10 19:58:15 +00:00
|
|
|
ret = ff_bprint_to_codecpar_extradata(st->codecpar, &header);
|
2012-12-29 21:09:59 +00:00
|
|
|
if (ret < 0)
|
2014-11-25 14:20:02 +00:00
|
|
|
goto fail;
|
2011-04-18 15:40:51 +00:00
|
|
|
|
|
|
|
/* SHIFT and TIMERES affect the whole script so packet timing can only be
|
|
|
|
* done in a second pass */
|
2012-06-15 16:57:39 +00:00
|
|
|
for (i = 0; i < jacosub->q.nb_subs; i++) {
|
2021-02-03 23:55:27 +00:00
|
|
|
AVPacket *sub = jacosub->q.subs[i];
|
2012-06-15 16:57:39 +00:00
|
|
|
read_ts(jacosub, sub->data, &sub->pts, &sub->duration);
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
2015-09-10 19:40:07 +00:00
|
|
|
ff_subtitles_queue_finalize(s, &jacosub->q);
|
2011-04-18 15:40:51 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-11-25 14:20:02 +00:00
|
|
|
fail:
|
|
|
|
jacosub_read_close(s);
|
|
|
|
return ret;
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int jacosub_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
JACOsubContext *jacosub = s->priv_data;
|
2012-06-15 16:57:39 +00:00
|
|
|
return ff_subtitles_queue_read_packet(&jacosub->q, pkt);
|
2011-04-18 15:40:51 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 20:43:54 +00:00
|
|
|
static int jacosub_read_seek(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
|
|
|
{
|
|
|
|
JACOsubContext *jacosub = s->priv_data;
|
|
|
|
return ff_subtitles_queue_seek(&jacosub->q, s, stream_index,
|
|
|
|
min_ts, ts, max_ts, flags);
|
|
|
|
}
|
|
|
|
|
2011-04-18 15:40:51 +00:00
|
|
|
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,
|
2012-11-23 20:43:54 +00:00
|
|
|
.read_seek2 = jacosub_read_seek,
|
2011-04-18 15:40:51 +00:00
|
|
|
.read_close = jacosub_read_close,
|
|
|
|
};
|