From faa94061dd7236cbaabd483e7b2df148cdbefb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= Date: Sat, 29 Dec 2012 00:04:44 +0100 Subject: Add SubViewer v1 subtitles demuxer and decoder. --- Changelog | 2 +- doc/general.texi | 1 + libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/avcodec.h | 1 + libavcodec/codec_desc.c | 6 ++ libavcodec/textdec.c | 18 +++++- libavcodec/version.h | 2 +- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/subviewer1dec.c | 124 ++++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- tests/fate/subtitles.mak | 3 + tests/ref/fate/sub-subviewer1 | 1 + 14 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 libavformat/subviewer1dec.c create mode 100644 tests/ref/fate/sub-subviewer1 diff --git a/Changelog b/Changelog index cc77eb2..4ebeae3 100644 --- a/Changelog +++ b/Changelog @@ -51,7 +51,7 @@ version : - pp (postproc) filter ported from MPlayer - NIST Sphere demuxer - av_basename and av_dirname -- MPL2, VPlayer, MPlayer, AQTitle and PJS subtitles demuxers and decoders +- MPL2, VPlayer, MPlayer, AQTitle, PJS and SubViewer v1 subtitles demuxers and decoders version 1.0: diff --git a/doc/general.texi b/doc/general.texi index 24870b6..ea513ff 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -933,6 +933,7 @@ performance on systems without hardware floating point support). @item SAMI @tab @tab X @tab @tab X @item SSA/ASS @tab X @tab X @tab X @tab X @item SubRip (SRT) @tab X @tab X @tab X @tab X +@item SubViewer v1 @tab @tab X @tab @tab X @item SubViewer @tab @tab X @tab @tab X @item TED Talks captions @tab @tab X @tab @tab X @item VobSub (IDX+SUB) @tab @tab X @tab @tab X diff --git a/libavcodec/Makefile b/libavcodec/Makefile index f8dd081..27c9a11 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -402,6 +402,7 @@ OBJS-$(CONFIG_SRT_DECODER) += srtdec.o ass.o OBJS-$(CONFIG_SRT_ENCODER) += srtenc.o ass_split.o OBJS-$(CONFIG_SUBRIP_DECODER) += srtdec.o ass.o OBJS-$(CONFIG_SUBRIP_ENCODER) += srtenc.o ass_split.o +OBJS-$(CONFIG_SUBVIEWER1_DECODER) += textdec.o ass.o OBJS-$(CONFIG_SUBVIEWER_DECODER) += subviewerdec.o ass.o OBJS-$(CONFIG_SUNRAST_DECODER) += sunrast.o OBJS-$(CONFIG_SUNRAST_ENCODER) += sunrastenc.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index d519a3e..987b877 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -456,6 +456,7 @@ void avcodec_register_all(void) REGISTER_ENCDEC (SRT, srt); REGISTER_ENCDEC (SUBRIP, subrip); REGISTER_DECODER(SUBVIEWER, subviewer); + REGISTER_DECODER(SUBVIEWER1, subviewer1); REGISTER_DECODER(TEXT, text); REGISTER_DECODER(VPLAYER, vplayer); REGISTER_DECODER(WEBVTT, webvtt); diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 782b46b..012a31c 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -463,6 +463,7 @@ enum AVCodecID { AV_CODEC_ID_JACOSUB = MKBETAG('J','S','U','B'), AV_CODEC_ID_SAMI = MKBETAG('S','A','M','I'), AV_CODEC_ID_REALTEXT = MKBETAG('R','T','X','T'), + AV_CODEC_ID_SUBVIEWER1 = MKBETAG('S','b','V','1'), AV_CODEC_ID_SUBVIEWER = MKBETAG('S','u','b','V'), AV_CODEC_ID_SUBRIP = MKBETAG('S','R','i','p'), AV_CODEC_ID_WEBVTT = MKBETAG('W','V','T','T'), diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 13ebcec..d721780 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -2441,6 +2441,12 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("RealText subtitle"), }, { + .id = AV_CODEC_ID_SUBVIEWER1, + .type = AVMEDIA_TYPE_SUBTITLE, + .name = "subviewer1", + .long_name = NULL_IF_CONFIG_SMALL("SubViewer v1 subtitle"), + }, + { .id = AV_CODEC_ID_SUBVIEWER, .type = AVMEDIA_TYPE_SUBTITLE, .name = "subviewer", diff --git a/libavcodec/textdec.c b/libavcodec/textdec.c index 32af644..9b97bac 100644 --- a/libavcodec/textdec.c +++ b/libavcodec/textdec.c @@ -128,7 +128,7 @@ AVCodec ff_text_decoder = { }; #endif -#if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER +#if CONFIG_VPLAYER_DECODER || CONFIG_PJS_DECODER || CONFIG_TEXT_DECODER static int linebreak_init(AVCodecContext *avctx) { @@ -169,4 +169,20 @@ AVCodec ff_pjs_decoder = { }; #endif +#if CONFIG_SUBVIEWER1_DECODER +#define subviewer1_options options +DECLARE_CLASS(subviewer1); + +AVCodec ff_subviewer1_decoder = { + .name = "subviewer1", + .priv_data_size = sizeof(TextContext), + .long_name = NULL_IF_CONFIG_SMALL("SubViewer1 subtitle"), + .type = AVMEDIA_TYPE_SUBTITLE, + .id = AV_CODEC_ID_SUBVIEWER1, + .decode = text_decode_frame, + .init = linebreak_init, + .priv_class = &subviewer1_decoder_class, +}; +#endif + #endif /* text subtitles with '|' line break */ diff --git a/libavcodec/version.h b/libavcodec/version.h index bdd94b4..af58490 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -29,7 +29,7 @@ #include "libavutil/avutil.h" #define LIBAVCODEC_VERSION_MAJOR 54 -#define LIBAVCODEC_VERSION_MINOR 83 +#define LIBAVCODEC_VERSION_MINOR 84 #define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ diff --git a/libavformat/Makefile b/libavformat/Makefile index ce1d8f4..d77b3bd 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -348,6 +348,7 @@ OBJS-$(CONFIG_SPDIF_MUXER) += spdif.o spdifenc.o OBJS-$(CONFIG_SRT_DEMUXER) += srtdec.o OBJS-$(CONFIG_SRT_MUXER) += srtenc.o OBJS-$(CONFIG_STR_DEMUXER) += psxstr.o +OBJS-$(CONFIG_SUBVIEWER1_DEMUXER) += subviewer1dec.o OBJS-$(CONFIG_SUBVIEWER_DEMUXER) += subviewerdec.o OBJS-$(CONFIG_SWF_DEMUXER) += swfdec.o swf.o OBJS-$(CONFIG_SWF_MUXER) += swfenc.o swf.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 42878ec..b3455b5 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -259,6 +259,7 @@ void av_register_all(void) REGISTER_MUXDEMUX(SPDIF, spdif); REGISTER_MUXDEMUX(SRT, srt); REGISTER_DEMUXER (STR, str); + REGISTER_DEMUXER (SUBVIEWER1, subviewer1); REGISTER_DEMUXER (SUBVIEWER, subviewer); REGISTER_MUXDEMUX(SWF, swf); REGISTER_DEMUXER (TAK, tak); diff --git a/libavformat/subviewer1dec.c b/libavformat/subviewer1dec.c new file mode 100644 index 0000000..0dc1942 --- /dev/null +++ b/libavformat/subviewer1dec.c @@ -0,0 +1,124 @@ +/* + * 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 + * SubViewer v1 subtitle demuxer + */ + +#include "avformat.h" +#include "internal.h" +#include "subtitles.h" + +typedef struct { + FFDemuxSubtitlesQueue q; +} SubViewer1Context; + +static int subviewer1_probe(AVProbeData *p) +{ + const unsigned char *ptr = p->buf; + + if (strstr(ptr, "******** START SCRIPT ********")) + return AVPROBE_SCORE_MAX / 2; + return 0; +} + +static int subviewer1_read_header(AVFormatContext *s) +{ + int delay = 0; + AVPacket *sub = NULL; + SubViewer1Context *subviewer1 = s->priv_data; + AVStream *st = avformat_new_stream(s, NULL); + + if (!st) + return AVERROR(ENOMEM); + avpriv_set_pts_info(st, 64, 1, 1); + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; + st->codec->codec_id = AV_CODEC_ID_SUBVIEWER1; + + while (!url_feof(s->pb)) { + char line[4096]; + int len = ff_get_line(s->pb, line, sizeof(line)); + int hh, mm, ss; + + if (!len) + break; + + if (!strncmp(line, "[DELAY]", 7)) { + ff_get_line(s->pb, line, sizeof(line)); + sscanf(line, "%d", &delay); + } + + if (sscanf(line, "[%d:%d:%d]", &hh, &mm, &ss) == 3) { + const int64_t pos = avio_tell(s->pb); + int64_t pts_start = hh*3600LL + mm*60LL + ss + delay; + + len = ff_get_line(s->pb, line, sizeof(line)); + line[strcspn(line, "\r\n")] = 0; + if (!*line) { + if (sub) + sub->duration = pts_start - sub->pts; + } else { + sub = ff_subtitles_queue_insert(&subviewer1->q, line, len, 0); + if (!sub) + return AVERROR(ENOMEM); + sub->pos = pos; + sub->pts = pts_start; + sub->duration = -1; + } + } + } + + ff_subtitles_queue_finalize(&subviewer1->q); + return 0; +} + +static int subviewer1_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + SubViewer1Context *subviewer1 = s->priv_data; + return ff_subtitles_queue_read_packet(&subviewer1->q, pkt); +} + +static int subviewer1_read_seek(AVFormatContext *s, int stream_index, + int64_t min_ts, int64_t ts, int64_t max_ts, int flags) +{ + SubViewer1Context *subviewer1 = s->priv_data; + return ff_subtitles_queue_seek(&subviewer1->q, s, stream_index, + min_ts, ts, max_ts, flags); +} + +static int subviewer1_read_close(AVFormatContext *s) +{ + SubViewer1Context *subviewer1 = s->priv_data; + ff_subtitles_queue_clean(&subviewer1->q); + return 0; +} + +AVInputFormat ff_subviewer1_demuxer = { + .name = "subviewer1", + .long_name = NULL_IF_CONFIG_SMALL("SubViewer v1 subtitle format"), + .priv_data_size = sizeof(SubViewer1Context), + .read_probe = subviewer1_probe, + .read_header = subviewer1_read_header, + .read_packet = subviewer1_read_packet, + .read_seek2 = subviewer1_read_seek, + .read_close = subviewer1_read_close, + .extensions = "sub", +}; diff --git a/libavformat/version.h b/libavformat/version.h index f3cc75a..90d47d0 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 55 +#define LIBAVFORMAT_VERSION_MINOR 56 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ diff --git a/tests/fate/subtitles.mak b/tests/fate/subtitles.mak index 3562dff..812873e 100644 --- a/tests/fate/subtitles.mak +++ b/tests/fate/subtitles.mak @@ -37,6 +37,9 @@ fate-sub-srt: CMD = md5 -i $(SAMPLES)/sub/SubRip_capability_tester.srt -f ass FATE_SUBTITLES-$(call ALLYES, MOV_DEMUXER MOVTEXT_DECODER SUBRIP_ENCODER) += fate-sub-subripenc fate-sub-subripenc: CMD = md5 -i $(SAMPLES)/sub/MovText_capability_tester.mp4 -scodec subrip -f srt +FATE_SUBTITLES_ASS-$(call DEMDEC, SUBVIEWER1, SUBVIEWER1) += fate-sub-subviewer1 +fate-sub-subviewer1: CMD = md5 -i $(SAMPLES)/sub/SubViewer1_capability_tester.sub -f ass + FATE_SUBTITLES_ASS-$(call DEMDEC, SUBVIEWER, SUBVIEWER) += fate-sub-subviewer fate-sub-subviewer: CMD = md5 -i $(SAMPLES)/sub/SubViewer_capability_tester.sub -f ass diff --git a/tests/ref/fate/sub-subviewer1 b/tests/ref/fate/sub-subviewer1 new file mode 100644 index 0000000..116fce7 --- /dev/null +++ b/tests/ref/fate/sub-subviewer1 @@ -0,0 +1 @@ +0c2096fedf7c971742b2e879bb303ce9 -- cgit v1.1