FFmpeg
Loading...
Searching...
No Matches
ttmlenc.c
Go to the documentation of this file.
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 "mux.h"
34#include "ttmlenc.h"
35#include "libavcodec/ttmlenc.h"
36#include "libavutil/internal.h"
37#include "libavutil/mem.h"
38
43
45 const char *tt_element_params;
46 const char *pre_body_elements;
47};
48
53
54static const char ttml_header_text[] =
55"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
56"<tt\n"
57"%s"
58" xml:lang=\"%s\">\n"
59"%s"
60" <body>\n"
61" <div>\n";
62
63static const char ttml_footer_text[] =
64" </div>\n"
65" </body>\n"
66"</tt>\n";
67
68static void ttml_write_time(AVIOContext *pb, const char tag[],
69 int64_t millisec)
70{
71 int64_t sec, min, hour;
72 sec = millisec / 1000;
73 millisec -= 1000 * sec;
74 min = sec / 60;
75 sec -= 60 * min;
76 hour = min / 60;
77 min -= 60 * hour;
78
79 avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"",
80 tag, hour, min, sec, millisec);
81}
82
84 AVCodecParameters *par, struct TTMLHeaderParameters *header_params)
85{
86 size_t additional_data_size =
88 char *value =
90 size_t value_size = av_strnlen(value, additional_data_size);
91 struct TTMLHeaderParameters local_params = { 0 };
92
93 if (!additional_data_size) {
94 // simple case, we don't have to go through local_params and just
95 // set default fall-back values (for old extradata format).
97 header_params->pre_body_elements = "";
98
99 return 0;
100 }
101
102 if (value_size == additional_data_size ||
103 value[value_size] != '\0')
104 return AVERROR_INVALIDDATA;
105
106 local_params.tt_element_params = value;
107
108 additional_data_size -= value_size + 1;
109 value += value_size + 1;
110 if (!additional_data_size)
111 return AVERROR_INVALIDDATA;
112
113 value_size = av_strnlen(value, additional_data_size);
114 if (value_size == additional_data_size ||
115 value[value_size] != '\0')
116 return AVERROR_INVALIDDATA;
117
118 local_params.pre_body_elements = value;
119
120 *header_params = local_params;
121
122 return 0;
123}
124
126{
127 TTMLMuxContext *ttml_ctx = ctx->priv_data;
128 AVStream *st = ctx->streams[0];
129 AVIOContext *pb = ctx->pb;
130
131 const AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,
132 0);
133
134 ttml_ctx->document_written = 0;
138
139 avpriv_set_pts_info(st, 64, 1, 1000);
140
141 if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH) {
142 struct TTMLHeaderParameters header_params;
143 char *printed_lang;
145 st->codecpar, &header_params);
146 if (ret < 0) {
148 "Failed to parse TTML header values from extradata: "
149 "%s!\n", av_err2str(ret));
150 return ret;
151 }
152
153 ret = av_escape(&printed_lang, lang ? lang->value : "", NULL,
155 if (ret < 0)
156 return ret;
157
159 header_params.tt_element_params,
160 printed_lang,
161 header_params.pre_body_elements);
162 av_free(printed_lang);
163 }
164
165 return 0;
166}
167
169{
170 TTMLMuxContext *ttml_ctx = ctx->priv_data;
171 AVIOContext *pb = ctx->pb;
172
173 switch (ttml_ctx->input_type) {
175 // write out a paragraph element with the given contents.
176 avio_printf(pb, " <p\n");
177 ttml_write_time(pb, " begin", pkt->pts);
178 avio_w8(pb, '\n');
179 ttml_write_time(pb, " end", pkt->pts + pkt->duration);
180 avio_printf(pb, ">");
181 avio_write(pb, pkt->data, pkt->size);
182 avio_printf(pb, "</p>\n");
183 break;
185 // dump the given document out as-is.
186 if (ttml_ctx->document_written) {
188 "Attempting to write multiple TTML documents into a "
189 "single document! The XML specification forbids this "
190 "as there has to be a single root tag.\n");
191 return AVERROR(EINVAL);
192 }
193 avio_write(pb, pkt->data, pkt->size);
194 ttml_ctx->document_written = 1;
195 break;
196 default:
198 "Internal error: invalid TTML input packet type: %d!\n",
199 ttml_ctx->input_type);
200 return AVERROR_BUG;
201 }
202
203 return 0;
204}
205
207{
208 TTMLMuxContext *ttml_ctx = ctx->priv_data;
209 AVIOContext *pb = ctx->pb;
210
211 if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
213
214 return 0;
215}
216
218 .p.name = "ttml",
219 .p.long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"),
220 .p.extensions = "ttml",
221 .p.mime_type = "text/ttml",
222 .priv_data_size = sizeof(TTMLMuxContext),
225 .p.video_codec = AV_CODEC_ID_NONE,
226 .p.audio_codec = AV_CODEC_ID_NONE,
227 .p.subtitle_codec = AV_CODEC_ID_TTML,
228 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
230 .write_header = ttml_write_header,
231 .write_packet = ttml_write_packet,
232 .write_trailer = ttml_write_trailer,
233};
const FFOutputFormat ff_ttml_muxer
Definition ttmlenc.c:217
static AVFormatContext * ctx
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:503
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:509
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition avformat.h:499
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define min(a, b)
static AVPacket * pkt
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
@ AV_CODEC_ID_TTML
Definition codec_id.h:596
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
size_t static size_t av_strnlen(const char *s, size_t len)
Get the count of continuous non zero chars starting from the beginning.
Definition avstring.h:141
int av_escape(char **dst, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape string in src, and put the escaped string in an allocated string in *dst, which must be freed ...
Definition avstring.c:326
#define AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES
Within AV_ESCAPE_MODE_XML, additionally escape double quotes for double quoted attributes.
Definition avstring.h:348
@ AV_ESCAPE_MODE_XML
Use XML non-markup character data escaping.
Definition avstring.h:318
#define TTML_DEFAULT_NAMESPACING
Definition ttmlenc.h:28
#define TTMLENC_EXTRADATA_SIGNATURE_SIZE
Definition ttmlenc.h:26
static int ttml_write_header(AVFormatContext *ctx)
Definition ttmlenc.c:125
TTMLPacketType
Definition ttmlenc.c:39
@ PACKET_TYPE_PARAGRAPH
Definition ttmlenc.c:40
@ PACKET_TYPE_DOCUMENT
Definition ttmlenc.c:41
static int ttml_write_trailer(AVFormatContext *ctx)
Definition ttmlenc.c:206
static int ttml_set_header_values_from_extradata(AVCodecParameters *par, struct TTMLHeaderParameters *header_params)
Definition ttmlenc.c:83
static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition ttmlenc.c:168
static const char ttml_footer_text[]
Definition ttmlenc.c:63
static const char ttml_header_text[]
Definition ttmlenc.c:54
static void ttml_write_time(AVIOContext *pb, const char tag[], int64_t millisec)
Definition ttmlenc.c:68
static unsigned int ff_is_ttml_stream_paragraph_based(const AVCodecParameters *codecpar)
Definition ttmlenc.h:28
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
Memory handling functions.
uint32_t tag
Definition movenc.c:2190
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1401
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:587
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVDictionary * metadata
Definition avformat.h:848
const char * pre_body_elements
Definition ttmlenc.c:46
const char * tt_element_params
Definition ttmlenc.c:45
enum TTMLPacketType input_type
Definition ttmlenc.c:50
unsigned int document_written
Definition ttmlenc.c:51
#define av_free(p)
#define av_log(a,...)