xref: /third_party/ffmpeg/libavformat/ttmlenc.c (revision cabdff1a)
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 "libavutil/avstring.h"
31#include "avformat.h"
32#include "internal.h"
33#include "ttmlenc.h"
34#include "libavcodec/ttmlenc.h"
35#include "libavutil/internal.h"
36
37enum TTMLPacketType {
38    PACKET_TYPE_PARAGRAPH,
39    PACKET_TYPE_DOCUMENT,
40};
41
42struct TTMLHeaderParameters {
43    const char *tt_element_params;
44    const char *pre_body_elements;
45};
46
47typedef struct TTMLMuxContext {
48    enum TTMLPacketType input_type;
49    unsigned int document_written;
50} TTMLMuxContext;
51
52static const char ttml_header_text[] =
53"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
54"<tt\n"
55"%s"
56"  xml:lang=\"%s\">\n"
57"%s"
58"  <body>\n"
59"    <div>\n";
60
61static const char ttml_footer_text[] =
62"    </div>\n"
63"  </body>\n"
64"</tt>\n";
65
66static void ttml_write_time(AVIOContext *pb, const char tag[],
67                            int64_t millisec)
68{
69    int64_t sec, min, hour;
70    sec = millisec / 1000;
71    millisec -= 1000 * sec;
72    min = sec / 60;
73    sec -= 60 * min;
74    hour = min / 60;
75    min -= 60 * hour;
76
77    avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"",
78                tag, hour, min, sec, millisec);
79}
80
81static int ttml_set_header_values_from_extradata(
82    AVCodecParameters *par, struct TTMLHeaderParameters *header_params)
83{
84    size_t additional_data_size =
85        par->extradata_size - TTMLENC_EXTRADATA_SIGNATURE_SIZE;
86    char *value =
87        (char *)par->extradata + TTMLENC_EXTRADATA_SIGNATURE_SIZE;
88    size_t value_size = av_strnlen(value, additional_data_size);
89    struct TTMLHeaderParameters local_params = { 0 };
90
91    if (!additional_data_size) {
92        // simple case, we don't have to go through local_params and just
93        // set default fall-back values (for old extradata format).
94        header_params->tt_element_params = ttml_default_namespacing;
95        header_params->pre_body_elements = "";
96
97        return 0;
98    }
99
100    if (value_size == additional_data_size ||
101        value[value_size] != '\0')
102        return AVERROR_INVALIDDATA;
103
104    local_params.tt_element_params = value;
105
106    additional_data_size -= value_size + 1;
107    value += value_size + 1;
108    if (!additional_data_size)
109        return AVERROR_INVALIDDATA;
110
111    value_size = av_strnlen(value, additional_data_size);
112    if (value_size == additional_data_size ||
113        value[value_size] != '\0')
114        return AVERROR_INVALIDDATA;
115
116    local_params.pre_body_elements = value;
117
118    *header_params = local_params;
119
120    return 0;
121}
122
123static int ttml_write_header(AVFormatContext *ctx)
124{
125    TTMLMuxContext *ttml_ctx = ctx->priv_data;
126    ttml_ctx->document_written = 0;
127
128    if (ctx->nb_streams != 1 ||
129        ctx->streams[0]->codecpar->codec_id != AV_CODEC_ID_TTML) {
130        av_log(ctx, AV_LOG_ERROR, "Exactly one TTML stream is required!\n");
131        return AVERROR(EINVAL);
132    }
133
134    {
135        AVStream    *st = ctx->streams[0];
136        AVIOContext *pb = ctx->pb;
137
138        AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,
139                                              0);
140        const char *printed_lang = (lang && lang->value) ? lang->value : "";
141
142        ttml_ctx->input_type = ff_is_ttml_stream_paragraph_based(st->codecpar) ?
143                               PACKET_TYPE_PARAGRAPH :
144                               PACKET_TYPE_DOCUMENT;
145
146        avpriv_set_pts_info(st, 64, 1, 1000);
147
148        if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH) {
149            struct TTMLHeaderParameters header_params;
150            int ret = ttml_set_header_values_from_extradata(
151                st->codecpar, &header_params);
152            if (ret < 0) {
153                av_log(ctx, AV_LOG_ERROR,
154                       "Failed to parse TTML header values from extradata: "
155                       "%s!\n", av_err2str(ret));
156                return ret;
157            }
158
159            avio_printf(pb, ttml_header_text,
160                        header_params.tt_element_params,
161                        printed_lang,
162                        header_params.pre_body_elements);
163        }
164    }
165
166    return 0;
167}
168
169static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt)
170{
171    TTMLMuxContext *ttml_ctx = ctx->priv_data;
172    AVIOContext    *pb       = ctx->pb;
173
174    switch (ttml_ctx->input_type) {
175    case PACKET_TYPE_PARAGRAPH:
176        // write out a paragraph element with the given contents.
177        avio_printf(pb,     "      <p\n");
178        ttml_write_time(pb, "        begin", pkt->pts);
179        avio_w8(pb, '\n');
180        ttml_write_time(pb, "        end",   pkt->pts + pkt->duration);
181        avio_printf(pb, ">");
182        avio_write(pb, pkt->data, pkt->size);
183        avio_printf(pb, "</p>\n");
184        break;
185    case PACKET_TYPE_DOCUMENT:
186        // dump the given document out as-is.
187        if (ttml_ctx->document_written) {
188            av_log(ctx, AV_LOG_ERROR,
189                   "Attempting to write multiple TTML documents into a "
190                   "single document! The XML specification forbids this "
191                   "as there has to be a single root tag.\n");
192            return AVERROR(EINVAL);
193        }
194        avio_write(pb, pkt->data, pkt->size);
195        ttml_ctx->document_written = 1;
196        break;
197    default:
198        av_log(ctx, AV_LOG_ERROR,
199               "Internal error: invalid TTML input packet type: %d!\n",
200               ttml_ctx->input_type);
201        return AVERROR_BUG;
202    }
203
204    return 0;
205}
206
207static int ttml_write_trailer(AVFormatContext *ctx)
208{
209    TTMLMuxContext *ttml_ctx = ctx->priv_data;
210    AVIOContext    *pb       = ctx->pb;
211
212    if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
213        avio_printf(pb, ttml_footer_text);
214
215    return 0;
216}
217
218const AVOutputFormat ff_ttml_muxer = {
219    .name              = "ttml",
220    .long_name         = NULL_IF_CONFIG_SMALL("TTML subtitle"),
221    .extensions        = "ttml",
222    .mime_type         = "text/ttml",
223    .priv_data_size    = sizeof(TTMLMuxContext),
224    .flags             = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
225                         AVFMT_TS_NONSTRICT,
226    .subtitle_codec    = AV_CODEC_ID_TTML,
227    .write_header      = ttml_write_header,
228    .write_packet      = ttml_write_packet,
229    .write_trailer     = ttml_write_trailer,
230};
231