FFmpeg  4.4.9
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 "avformat.h"
31 #include "internal.h"
32 #include "libavcodec/ttmlenc.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/mem.h"
36 
40 };
41 
42 typedef struct TTMLMuxContext {
44  unsigned int document_written;
46 
47 static const char ttml_header_text[] =
48 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
49 "<tt\n"
50 " xmlns=\"http://www.w3.org/ns/ttml\"\n"
51 " xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"\n"
52 " xmlns:tts=\"http://www.w3.org/ns/ttml#styling\"\n"
53 " xml:lang=\"%s\">\n"
54 " <body>\n"
55 " <div>\n";
56 
57 static const char ttml_footer_text[] =
58 " </div>\n"
59 " </body>\n"
60 "</tt>\n";
61 
62 static void ttml_write_time(AVIOContext *pb, const char tag[],
63  int64_t millisec)
64 {
65  int64_t sec, min, hour;
66  sec = millisec / 1000;
67  millisec -= 1000 * sec;
68  min = sec / 60;
69  sec -= 60 * min;
70  hour = min / 60;
71  min -= 60 * hour;
72 
73  avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"",
74  tag, hour, min, sec, millisec);
75 }
76 
78 {
79  TTMLMuxContext *ttml_ctx = ctx->priv_data;
80  ttml_ctx->document_written = 0;
81 
82  if (ctx->nb_streams != 1 ||
84  av_log(ctx, AV_LOG_ERROR, "Exactly one TTML stream is required!\n");
85  return AVERROR(EINVAL);
86  }
87 
88  {
89  AVStream *st = ctx->streams[0];
90  AVIOContext *pb = ctx->pb;
91 
92  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,
93  0);
94 
95  // Not perfect, but decide whether the packet is a document or not
96  // by the existence of the lavc ttmlenc extradata.
97  ttml_ctx->input_type = (st->codecpar->extradata &&
99  !memcmp(st->codecpar->extradata,
104 
105  avpriv_set_pts_info(st, 64, 1, 1000);
106 
107  if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH) {
108  char *printed_lang;
109  int ret = av_escape(&printed_lang, lang ? lang->value : "", NULL,
111  if (ret < 0)
112  return ret;
113 
114  avio_printf(pb, ttml_header_text, printed_lang);
115  av_free(printed_lang);
116  }
117  }
118 
119  return 0;
120 }
121 
123 {
124  TTMLMuxContext *ttml_ctx = ctx->priv_data;
125  AVIOContext *pb = ctx->pb;
126 
127  switch (ttml_ctx->input_type) {
129  // write out a paragraph element with the given contents.
130  avio_printf(pb, " <p\n");
131  ttml_write_time(pb, " begin", pkt->pts);
132  avio_w8(pb, '\n');
133  ttml_write_time(pb, " end", pkt->pts + pkt->duration);
134  avio_printf(pb, ">");
135  avio_write(pb, pkt->data, pkt->size);
136  avio_printf(pb, "</p>\n");
137  break;
139  // dump the given document out as-is.
140  if (ttml_ctx->document_written) {
142  "Attempting to write multiple TTML documents into a "
143  "single document! The XML specification forbids this "
144  "as there has to be a single root tag.\n");
145  return AVERROR(EINVAL);
146  }
147  avio_write(pb, pkt->data, pkt->size);
148  ttml_ctx->document_written = 1;
149  break;
150  default:
152  "Internal error: invalid TTML input packet type: %d!\n",
153  ttml_ctx->input_type);
154  return AVERROR_BUG;
155  }
156 
157  return 0;
158 }
159 
161 {
162  TTMLMuxContext *ttml_ctx = ctx->priv_data;
163  AVIOContext *pb = ctx->pb;
164 
165  if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
167 
168  return 0;
169 }
170 
172  .name = "ttml",
173  .long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"),
174  .extensions = "ttml",
175  .mime_type = "text/ttml",
176  .priv_data_size = sizeof(TTMLMuxContext),
179  .subtitle_codec = AV_CODEC_ID_TTML,
183 };
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:465
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:472
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:461
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:208
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:230
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:740
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
@ AV_CODEC_ID_TTML
Definition: codec_id.h:548
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:40
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
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:338
#define AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES
Within AV_ESCAPE_MODE_XML, additionally escape double quotes for double quoted attributes.
Definition: avstring.h:357
@ AV_ESCAPE_MODE_XML
Use XML non-markup character data escaping.
Definition: avstring.h:327
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4944
static int ttml_write_header(AVFormatContext *ctx)
Definition: ttmlenc.c:77
TTMLPacketType
Definition: ttmlenc.c:37
@ PACKET_TYPE_PARAGRAPH
Definition: ttmlenc.c:38
@ PACKET_TYPE_DOCUMENT
Definition: ttmlenc.c:39
static int ttml_write_trailer(AVFormatContext *ctx)
Definition: ttmlenc.c:160
static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: ttmlenc.c:122
static const char ttml_footer_text[]
Definition: ttmlenc.c:57
AVOutputFormat ff_ttml_muxer
Definition: ttmlenc.c:171
static const char ttml_header_text[]
Definition: ttmlenc.c:47
static void ttml_write_time(AVIOContext *pb, const char tag[], int64_t millisec)
Definition: ttmlenc.c:62
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:117
Memory handling functions.
uint32_t tag
Definition: movenc.c:1611
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
char * value
Definition: dict.h:83
Format I/O context.
Definition: avformat.h:1232
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
AVIOContext * pb
I/O context.
Definition: avformat.h:1274
void * priv_data
Format private data.
Definition: avformat.h:1260
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
Bytestream IO Context.
Definition: avio.h:161
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVDictionary * metadata
Definition: avformat.h:937
enum TTMLPacketType input_type
Definition: ttmlenc.c:43
unsigned int document_written
Definition: ttmlenc.c:44
#define av_free(p)
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
#define TTMLENC_EXTRADATA_SIGNATURE
Definition: ttmlenc.h:25
#define TTMLENC_EXTRADATA_SIGNATURE_SIZE
Definition: ttmlenc.h:26
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
float min