[FFmpeg-devel] [PATCH 1/4] add SRT parser, demuxer and muxer
Aurelien Jacobs
aurel
Tue Jul 6 22:54:48 CEST 2010
---
libavcodec/Makefile | 1 +
libavcodec/allcodecs.c | 1 +
libavcodec/avcodec.h | 1 +
libavcodec/srt_parser.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++
libavformat/Makefile | 2 +
libavformat/allformats.c | 1 +
libavformat/raw.c | 12 +++++++
libavformat/srtdec.c | 68 ++++++++++++++++++++++++++++++++++++++
8 files changed, 168 insertions(+), 0 deletions(-)
create mode 100644 libavcodec/srt_parser.c
create mode 100644 libavformat/srtdec.c
-------------- next part --------------
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index f57532c..dc28ccb 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -577,6 +577,7 @@ OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \
mpeg12.o mpeg12data.o \
mpegvideo.o error_resilience.o
OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o
+OBJS-$(CONFIG_SRT_PARSER) += srt_parser.o
OBJS-$(CONFIG_VC1_PARSER) += vc1_parser.o vc1.o vc1data.o \
msmpeg4.o msmpeg4data.o mpeg4video.o \
h263.o mpegvideo.o error_resilience.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index ccede8b..48a7bb3 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -373,6 +373,7 @@ void avcodec_register_all(void)
REGISTER_PARSER (MPEGAUDIO, mpegaudio);
REGISTER_PARSER (MPEGVIDEO, mpegvideo);
REGISTER_PARSER (PNM, pnm);
+ REGISTER_PARSER (SRT, srt);
REGISTER_PARSER (VC1, vc1);
REGISTER_PARSER (VP3, vp3);
REGISTER_PARSER (VP8, vp8);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 11ef87b..2a03869 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -347,6 +347,7 @@ enum CodecID {
CODEC_ID_MOV_TEXT,
CODEC_ID_HDMV_PGS_SUBTITLE,
CODEC_ID_DVB_TELETEXT,
+ CODEC_ID_SRT,
/* other specific kind of codecs (generally used for attachments) */
CODEC_ID_TTF= 0x18000,
diff --git a/libavcodec/srt_parser.c b/libavcodec/srt_parser.c
new file mode 100644
index 0000000..13a4cae
--- /dev/null
+++ b/libavcodec/srt_parser.c
@@ -0,0 +1,82 @@
+/*
+ * SRT parser
+ * Copyright (C) 2010 Aurelien Jacobs <aurel at gnuage.org>
+ *
+ * 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 <string.h>
+#include <stdio.h>
+#include "parser.h"
+
+static int64_t get_pts(const uint8_t *buf)
+{
+ int i, v, hour, min, sec, hsec;
+
+ for (i=0; i<2; i++) {
+ if (sscanf(buf, "%d:%2d:%2d,%3d --> %*d:%*2d:%*2d,%3d",
+ &hour, &min, &sec, &hsec, &v) == 5) {
+ min += 60*hour;
+ sec += 60*min;
+ return sec*1000+hsec;
+ }
+ buf += strcspn(buf, "\n") + 1;
+ }
+ return AV_NOPTS_VALUE;
+}
+
+static inline int is_eol(uint8_t c)
+{
+ return c == '\r' || c == '\n';
+}
+
+static int srt_find_frame_end(const uint8_t *buf)
+{
+ const uint8_t *ptr = buf;
+
+ while (*ptr && !is_eol(*ptr)) /* not a blank line ? */
+ ptr += strcspn(ptr, "\n") + 1; /* eat the whole line */
+ if (!*ptr) /* incomplete event ? */
+ return END_NOT_FOUND;
+ while (is_eol(*++ptr)); /* eat consecutive blank lines */
+ return ptr - buf;
+}
+
+static int srt_parse(AVCodecParserContext *s, AVCodecContext *avctx,
+ const uint8_t **poutbuf, int *poutbuf_size,
+ const uint8_t *buf, int buf_size)
+{
+ ParseContext *pc = s->priv_data;
+ int next = srt_find_frame_end(buf);
+
+ if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
+ *poutbuf = NULL;
+ *poutbuf_size = 0;
+ return buf_size;
+ }
+ s->pts = s->dts = get_pts(buf);
+ *poutbuf = buf;
+ *poutbuf_size = buf_size;
+ return next;
+}
+
+AVCodecParser srt_parser = {
+ .codec_ids = { CODEC_ID_SRT },
+ .priv_data_size = sizeof(ParseContext),
+ .parser_parse = srt_parse,
+ .parser_close = ff_parse_close,
+};
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 4249428..853f264 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -239,6 +239,8 @@ OBJS-$(CONFIG_SOL_DEMUXER) += sol.o raw.o
OBJS-$(CONFIG_SOX_DEMUXER) += soxdec.o raw.o
OBJS-$(CONFIG_SOX_MUXER) += soxenc.o
OBJS-$(CONFIG_SPDIF_MUXER) += spdif.o
+OBJS-$(CONFIG_SRT_DEMUXER) += srtdec.o
+OBJS-$(CONFIG_SRT_MUXER) += raw.o
OBJS-$(CONFIG_STR_DEMUXER) += psxstr.o
OBJS-$(CONFIG_SWF_DEMUXER) += swfdec.o
OBJS-$(CONFIG_SWF_MUXER) += swfenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 94ab78c..151f24d 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -188,6 +188,7 @@ void av_register_all(void)
REGISTER_DEMUXER (SOL, sol);
REGISTER_MUXDEMUX (SOX, sox);
REGISTER_MUXER (SPDIF, spdif);
+ REGISTER_MUXDEMUX (SRT, srt);
REGISTER_DEMUXER (STR, str);
REGISTER_MUXDEMUX (SWF, swf);
REGISTER_MUXER (TG2, tg2);
diff --git a/libavformat/raw.c b/libavformat/raw.c
index 0c83c42..dc48c91 100644
--- a/libavformat/raw.c
+++ b/libavformat/raw.c
@@ -1082,6 +1082,18 @@ AVOutputFormat mlp_muxer = {
};
#endif
+#if CONFIG_SRT_MUXER
+AVOutputFormat srt_muxer = {
+ .name = "srt",
+ .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
+ .mime_type = "application/x-subrip",
+ .extensions = "srt",
+ .write_packet = raw_write_packet,
+ .flags = AVFMT_NOTIMESTAMPS,
+ .subtitle_codec = CODEC_ID_SRT,
+};
+#endif
+
#if CONFIG_TRUEHD_DEMUXER
AVInputFormat truehd_demuxer = {
"truehd",
diff --git a/libavformat/srtdec.c b/libavformat/srtdec.c
new file mode 100644
index 0000000..5d74c43
--- /dev/null
+++ b/libavformat/srtdec.c
@@ -0,0 +1,68 @@
+/*
+ * SubRip subtitle demuxer
+ * Copyright (c) 2010 Aurelien Jacobs <aurel at gnuage.org>
+ *
+ * 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 "libavutil/intreadwrite.h"
+
+#define SRT_PACKET_SIZE 16384
+
+static int srt_probe(AVProbeData *p)
+{
+ unsigned char *ptr = p->buf;
+ int i, v, num = 0;
+
+ if (AV_RB24(ptr) == 0xEFBBBF)
+ ptr += 3; /* skip UTF-8 BOM */
+
+ for (i=0; i<2; i++) {
+ if (num == i &&
+ sscanf(ptr, "%*d:%*2d:%*2d,%*3d --> %*d:%*2d:%*2d,%3d", &v) == 1)
+ return AVPROBE_SCORE_MAX;
+ num = atoi(ptr);
+ ptr += strcspn(ptr, "\n") + 1;
+ }
+ return 0;
+}
+
+static int srt_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+ AVStream *st = av_new_stream(s, 0);
+ if (!st)
+ return -1;
+ av_set_pts_info(st, 64, 1, 1000);
+ st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+ st->codec->codec_id = CODEC_ID_SRT;
+ st->need_parsing = AVSTREAM_PARSE_FULL;
+ return 0;
+}
+
+static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ return av_get_packet(s->pb, pkt, SRT_PACKET_SIZE);
+}
+
+AVInputFormat srt_demuxer = {
+ .name = "srt",
+ .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
+ .read_probe = srt_probe,
+ .read_header = srt_read_header,
+ .read_packet = srt_read_packet,
+};
More information about the ffmpeg-devel
mailing list