aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/supenc.c
diff options
context:
space:
mode:
authorPetri Hintukainen <phintuka@users.sourceforge.net>2014-09-30 11:01:17 +0300
committerPaul B Mahol <onemda@gmail.com>2017-09-09 12:07:14 +0200
commit7a6bd541528b4d00b52d422d02f01d42346e68df (patch)
tree66a826c500b2c652d08ff54b8a19745d47cdf22d /libavformat/supenc.c
parentab96e2ca81d7dd7d02c2b2176fc3dac85d9e8cb9 (diff)
downloadffmpeg-7a6bd541528b4d00b52d422d02f01d42346e68df.tar.gz
Add SUP/PGS subtitle muxer
Fixes ticket #2208
Diffstat (limited to 'libavformat/supenc.c')
-rw-r--r--libavformat/supenc.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/libavformat/supenc.c b/libavformat/supenc.c
new file mode 100644
index 0000000000..f5f6b58c87
--- /dev/null
+++ b/libavformat/supenc.c
@@ -0,0 +1,96 @@
+/*
+ * SUP muxer
+ * Copyright (c) 2014 Petri Hintukainen <phintuka@users.sourceforge.net>
+ *
+ * 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 "internal.h"
+#include "libavutil/intreadwrite.h"
+
+#define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
+
+static int sup_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ uint8_t *data = pkt->data;
+ size_t size = pkt->size;
+ uint32_t pts = 0, dts = 0;
+
+ if (pkt->pts != AV_NOPTS_VALUE) {
+ pts = (uint32_t)pkt->pts;
+ }
+ if (pkt->dts != AV_NOPTS_VALUE) {
+ dts = (uint32_t)pkt->dts;
+ }
+
+ /*
+ Split frame to segments.
+ mkvmerge stores multiple segments in one frame.
+ */
+ while (size > 2) {
+ size_t len = AV_RB16(data + 1) + 3;
+
+ if (len > size) {
+ av_log(s, AV_LOG_ERROR, "Not enough data, skipping %d bytes\n",
+ (int)size);
+ return AVERROR_INVALIDDATA;
+ }
+
+ /* header */
+ avio_wb16(s->pb, SUP_PGS_MAGIC);
+ avio_wb32(s->pb, pts);
+ avio_wb32(s->pb, dts);
+
+ avio_write(s->pb, data, len);
+
+ data += len;
+ size -= len;
+ }
+
+ if (size > 0) {
+ av_log(s, AV_LOG_ERROR, "Skipping %d bytes after last segment in frame\n",
+ (int)size);
+ return AVERROR_INVALIDDATA;
+ }
+
+ return 0;
+}
+
+static int sup_write_header(AVFormatContext *s)
+{
+ if (s->nb_streams != 1) {
+ av_log(s, AV_LOG_ERROR, "%s files have exactly one stream\n",
+ s->oformat->name);
+ return AVERROR(EINVAL);
+ }
+
+ avpriv_set_pts_info(s->streams[0], 32, 1, 90000);
+
+ return 0;
+}
+
+AVOutputFormat ff_sup_muxer = {
+ .name = "sup",
+ .long_name = NULL_IF_CONFIG_SMALL("raw HDMV Presentation Graphic Stream subtitles"),
+ .extensions = "sup",
+ .mime_type = "application/x-pgs",
+ .subtitle_codec = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
+ .write_header = sup_write_header,
+ .write_packet = sup_write_packet,
+ .flags = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
+};