1 /*
2 * TTML subtitle muxer
3 * Copyright (c) 2020 24i
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * TTML subtitle muxer
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
28 */
29
30 #include "avformat.h"
31 #include "internal.h"
32 #include "libavcodec/ttmlenc.h"
33 #include "libavutil/internal.h"
34
35 enum TTMLPacketType {
36 PACKET_TYPE_PARAGRAPH,
37 PACKET_TYPE_DOCUMENT,
38 };
39
40 typedef struct TTMLMuxContext {
41 enum TTMLPacketType input_type;
42 unsigned int document_written;
43 } TTMLMuxContext;
44
45 static const char ttml_header_text[] =
46 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
47 "<tt\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"
51 " xml:lang=\"%s\">\n"
52 " <body>\n"
53 " <div>\n";
54
55 static const char ttml_footer_text[] =
56 " </div>\n"
57 " </body>\n"
58 "</tt>\n";
59
ttml_write_time(AVIOContext * pb,const char tag[],int64_t millisec)60 static void ttml_write_time(AVIOContext *pb, const char tag[],
61 int64_t millisec)
62 {
63 int64_t sec, min, hour;
64 sec = millisec / 1000;
65 millisec -= 1000 * sec;
66 min = sec / 60;
67 sec -= 60 * min;
68 hour = min / 60;
69 min -= 60 * hour;
70
71 avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"",
72 tag, hour, min, sec, millisec);
73 }
74
ttml_write_header(AVFormatContext * ctx)75 static int ttml_write_header(AVFormatContext *ctx)
76 {
77 TTMLMuxContext *ttml_ctx = ctx->priv_data;
78 ttml_ctx->document_written = 0;
79
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);
84 }
85
86 {
87 AVStream *st = ctx->streams[0];
88 AVIOContext *pb = ctx->pb;
89
90 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,
91 0);
92 const char *printed_lang = (lang && lang->value) ? lang->value : "";
93
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;
103
104 avpriv_set_pts_info(st, 64, 1, 1000);
105
106 if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
107 avio_printf(pb, ttml_header_text, printed_lang);
108 }
109
110 return 0;
111 }
112
ttml_write_packet(AVFormatContext * ctx,AVPacket * pkt)113 static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt)
114 {
115 TTMLMuxContext *ttml_ctx = ctx->priv_data;
116 AVIOContext *pb = ctx->pb;
117
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);
123 avio_w8(pb, '\n');
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");
128 break;
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);
137 }
138 avio_write(pb, pkt->data, pkt->size);
139 ttml_ctx->document_written = 1;
140 break;
141 default:
142 av_log(ctx, AV_LOG_ERROR,
143 "Internal error: invalid TTML input packet type: %d!\n",
144 ttml_ctx->input_type);
145 return AVERROR_BUG;
146 }
147
148 return 0;
149 }
150
ttml_write_trailer(AVFormatContext * ctx)151 static int ttml_write_trailer(AVFormatContext *ctx)
152 {
153 TTMLMuxContext *ttml_ctx = ctx->priv_data;
154 AVIOContext *pb = ctx->pb;
155
156 if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
157 avio_printf(pb, ttml_footer_text);
158
159 return 0;
160 }
161
162 AVOutputFormat ff_ttml_muxer = {
163 .name = "ttml",
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 |
169 AVFMT_TS_NONSTRICT,
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,
174 };
175