ffmpeg/libavformat/jacosubdec.c

261 lines
7.2 KiB
C
Raw Normal View History

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"
#include "subtitles.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;
FFDemuxSubtitlesQueue q;
2011-04-18 15:40:51 +00:00
} 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) {
while (jss_whitespace(*ptr))
ptr++;
if (*ptr != '#' && *ptr != '\n') {
if (timed_line(ptr))
return AVPROBE_SCORE_MAX/2 + 1;
return 0;
}
2011-04-18 15:40:51 +00:00
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)
{
JACOsubContext *jacosub = s->priv_data;
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,
int64_t *start, int *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
int ts_start, ts_end;
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) {
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 */
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:
ts_start = (ts_start + jacosub->shift) * 100 / jacosub->timeres;
ts_end = (ts_end + jacosub->shift) * 100 / jacosub->timeres;
*start = ts_start;
*duration = ts_start + ts_end;
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;
#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 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;
Merge commit '36ef5369ee9b336febc2c270f8718cec4476cb85' * commit '36ef5369ee9b336febc2c270f8718cec4476cb85': Replace all CODEC_ID_* with AV_CODEC_ID_* lavc: add AV prefix to codec ids. Conflicts: doc/APIchanges doc/examples/decoding_encoding.c doc/examples/muxing.c ffmpeg.c ffprobe.c ffserver.c libavcodec/8svx.c libavcodec/avcodec.h libavcodec/dnxhd_parser.c libavcodec/dvdsubdec.c libavcodec/error_resilience.c libavcodec/h263dec.c libavcodec/libvorbisenc.c libavcodec/mjpeg_parser.c libavcodec/mjpegenc.c libavcodec/mpeg12.c libavcodec/mpeg4videodec.c libavcodec/mpegvideo.c libavcodec/mpegvideo_enc.c libavcodec/pcm.c libavcodec/r210dec.c libavcodec/utils.c libavcodec/v210dec.c libavcodec/version.h libavdevice/alsa-audio-dec.c libavdevice/bktr.c libavdevice/v4l2.c libavformat/asfdec.c libavformat/asfenc.c libavformat/avformat.h libavformat/avidec.c libavformat/caf.c libavformat/electronicarts.c libavformat/flacdec.c libavformat/flvdec.c libavformat/flvenc.c libavformat/framecrcenc.c libavformat/img2.c libavformat/img2dec.c libavformat/img2enc.c libavformat/ipmovie.c libavformat/isom.c libavformat/matroska.c libavformat/matroskadec.c libavformat/matroskaenc.c libavformat/mov.c libavformat/movenc.c libavformat/mp3dec.c libavformat/mpeg.c libavformat/mpegts.c libavformat/mxf.c libavformat/mxfdec.c libavformat/mxfenc.c libavformat/nsvdec.c libavformat/nut.c libavformat/oggenc.c libavformat/pmpdec.c libavformat/rawdec.c libavformat/rawenc.c libavformat/riff.c libavformat/sdp.c libavformat/utils.c libavformat/vocenc.c libavformat/wtv.c libavformat/xmv.c Merged-by: Michael Niedermayer <michaelni@gmx.at>
2012-08-07 20:45:46 +00:00
st->codec->codec_id = AV_CODEC_ID_JACOSUB;
2011-04-18 15:40:51 +00:00
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);
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)) {
AVPacket *sub;
sub = ff_subtitles_queue_insert(&jacosub->q, line, len, merge_line);
if (!sub)
return AVERROR(ENOMEM);
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);
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 */
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->q.nb_subs; i++) {
AVPacket *sub = &jacosub->q.subs[i];
read_ts(jacosub, sub->data, &sub->pts, &sub->duration);
2011-04-18 15:40:51 +00:00
}
ff_subtitles_queue_finalize(&jacosub->q);
2011-04-18 15:40:51 +00:00
return 0;
}
static int jacosub_read_packet(AVFormatContext *s, AVPacket *pkt)
{
JACOsubContext *jacosub = s->priv_data;
return ff_subtitles_queue_read_packet(&jacosub->q, pkt);
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,
.read_close = jacosub_read_close,
.flags = AVFMT_GENERIC_INDEX,
};