3 * Copyright (c) 2020 24i
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @see https://www.w3.org/TR/ttml1/
26 * @see https://www.w3.org/TR/ttml2/
27 * @see https://www.w3.org/TR/ttml-imsc/rec
32 #include "libavcodec/ttmlenc.h"
33 #include "libavutil/internal.h"
36 PACKET_TYPE_PARAGRAPH,
40 typedef struct TTMLMuxContext {
41 enum TTMLPacketType input_type;
42 unsigned int document_written;
45 static const char ttml_header_text[] =
46 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
48 " xmlns=\"http://www.w3.org/ns/ttml\"\n"
49 " xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"\n"
50 " xmlns:tts=\"http://www.w3.org/ns/ttml#styling\"\n"
55 static const char ttml_footer_text[] =
60 static void ttml_write_time(AVIOContext *pb, const char tag[],
63 int64_t sec, min, hour;
64 sec = millisec / 1000;
65 millisec -= 1000 * sec;
71 avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"",
72 tag, hour, min, sec, millisec);
75 static int ttml_write_header(AVFormatContext *ctx)
77 TTMLMuxContext *ttml_ctx = ctx->priv_data;
78 ttml_ctx->document_written = 0;
80 if (ctx->nb_streams != 1 ||
81 ctx->streams[0]->codecpar->codec_id != AV_CODEC_ID_TTML) {
82 av_log(ctx, AV_LOG_ERROR, "Exactly one TTML stream is required!\n");
83 return AVERROR(EINVAL);
87 AVStream *st = ctx->streams[0];
88 AVIOContext *pb = ctx->pb;
90 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,
92 const char *printed_lang = (lang && lang->value) ? lang->value : "";
94 // Not perfect, but decide whether the packet is a document or not
95 // by the existence of the lavc ttmlenc extradata.
96 ttml_ctx->input_type = (st->codecpar->extradata &&
97 st->codecpar->extradata_size >= TTMLENC_EXTRADATA_SIGNATURE_SIZE &&
98 !memcmp(st->codecpar->extradata,
99 TTMLENC_EXTRADATA_SIGNATURE,
100 TTMLENC_EXTRADATA_SIGNATURE_SIZE)) ?
101 PACKET_TYPE_PARAGRAPH :
102 PACKET_TYPE_DOCUMENT;
104 avpriv_set_pts_info(st, 64, 1, 1000);
106 if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
107 avio_printf(pb, ttml_header_text, printed_lang);
113 static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt)
115 TTMLMuxContext *ttml_ctx = ctx->priv_data;
116 AVIOContext *pb = ctx->pb;
118 switch (ttml_ctx->input_type) {
119 case PACKET_TYPE_PARAGRAPH:
120 // write out a paragraph element with the given contents.
121 avio_printf(pb, " <p\n");
122 ttml_write_time(pb, " begin", pkt->pts);
124 ttml_write_time(pb, " end", pkt->pts + pkt->duration);
125 avio_printf(pb, ">");
126 avio_write(pb, pkt->data, pkt->size);
127 avio_printf(pb, "</p>\n");
129 case PACKET_TYPE_DOCUMENT:
130 // dump the given document out as-is.
131 if (ttml_ctx->document_written) {
132 av_log(ctx, AV_LOG_ERROR,
133 "Attempting to write multiple TTML documents into a "
134 "single document! The XML specification forbids this "
135 "as there has to be a single root tag.\n");
136 return AVERROR(EINVAL);
138 avio_write(pb, pkt->data, pkt->size);
139 ttml_ctx->document_written = 1;
142 av_log(ctx, AV_LOG_ERROR,
143 "Internal error: invalid TTML input packet type: %d!\n",
144 ttml_ctx->input_type);
151 static int ttml_write_trailer(AVFormatContext *ctx)
153 TTMLMuxContext *ttml_ctx = ctx->priv_data;
154 AVIOContext *pb = ctx->pb;
156 if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
157 avio_printf(pb, ttml_footer_text);
162 AVOutputFormat ff_ttml_muxer = {
164 .long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"),
165 .extensions = "ttml",
166 .mime_type = "text/ttml",
167 .priv_data_size = sizeof(TTMLMuxContext),
168 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
170 .subtitle_codec = AV_CODEC_ID_TTML,
171 .write_header = ttml_write_header,
172 .write_packet = ttml_write_packet,
173 .write_trailer = ttml_write_trailer,