FFmpeg
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 "libavcodec/ttmlenc.h"
34 #include "libavutil/internal.h"
35 
39 };
40 
42  const char *tt_element_params;
43  const char *pre_body_elements;
44 };
45 
46 typedef struct TTMLMuxContext {
48  unsigned int document_written;
50 
51 static const char ttml_header_text[] =
52 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
53 "<tt\n"
54 "%s"
55 " xml:lang=\"%s\">\n"
56 "%s"
57 " <body>\n"
58 " <div>\n";
59 
60 static const char ttml_footer_text[] =
61 " </div>\n"
62 " </body>\n"
63 "</tt>\n";
64 
65 static void ttml_write_time(AVIOContext *pb, const char tag[],
66  int64_t millisec)
67 {
68  int64_t sec, min, hour;
69  sec = millisec / 1000;
70  millisec -= 1000 * sec;
71  min = sec / 60;
72  sec -= 60 * min;
73  hour = min / 60;
74  min -= 60 * hour;
75 
76  avio_printf(pb, "%s=\"%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64"\"",
77  tag, hour, min, sec, millisec);
78 }
79 
81  AVCodecParameters *par, struct TTMLHeaderParameters *header_params)
82 {
83  size_t additional_data_size =
85  char *value =
87  size_t value_size = av_strnlen(value, additional_data_size);
88  struct TTMLHeaderParameters local_params = { 0 };
89 
90  if (!additional_data_size) {
91  // simple case, we don't have to go through local_params and just
92  // set default fall-back values (for old extradata format).
94  header_params->pre_body_elements = "";
95 
96  return 0;
97  }
98 
99  if (value_size == additional_data_size ||
100  value[value_size] != '\0')
101  return AVERROR_INVALIDDATA;
102 
103  local_params.tt_element_params = value;
104 
105  additional_data_size -= value_size + 1;
106  value += value_size + 1;
107  if (!additional_data_size)
108  return AVERROR_INVALIDDATA;
109 
110  value_size = av_strnlen(value, additional_data_size);
111  if (value_size == additional_data_size ||
112  value[value_size] != '\0')
113  return AVERROR_INVALIDDATA;
114 
115  local_params.pre_body_elements = value;
116 
117  *header_params = local_params;
118 
119  return 0;
120 }
121 
123 {
124  TTMLMuxContext *ttml_ctx = ctx->priv_data;
125  ttml_ctx->document_written = 0;
126 
127  if (ctx->nb_streams != 1 ||
129  av_log(ctx, AV_LOG_ERROR, "Exactly one TTML stream is required!\n");
130  return AVERROR(EINVAL);
131  }
132 
133  {
134  AVStream *st = ctx->streams[0];
135  AVIOContext *pb = ctx->pb;
136 
137  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,
138  0);
139  const char *printed_lang = (lang && lang->value) ? lang->value : "";
140 
141  // Not perfect, but decide whether the packet is a document or not
142  // by the existence of the lavc ttmlenc extradata.
143  ttml_ctx->input_type = (st->codecpar->extradata &&
145  !memcmp(st->codecpar->extradata,
150 
151  avpriv_set_pts_info(st, 64, 1, 1000);
152 
153  if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH) {
154  struct TTMLHeaderParameters header_params;
156  st->codecpar, &header_params);
157  if (ret < 0) {
159  "Failed to parse TTML header values from extradata: "
160  "%s!\n", av_err2str(ret));
161  return ret;
162  }
163 
165  header_params.tt_element_params,
166  printed_lang,
167  header_params.pre_body_elements);
168  }
169  }
170 
171  return 0;
172 }
173 
175 {
176  TTMLMuxContext *ttml_ctx = ctx->priv_data;
177  AVIOContext *pb = ctx->pb;
178 
179  switch (ttml_ctx->input_type) {
181  // write out a paragraph element with the given contents.
182  avio_printf(pb, " <p\n");
183  ttml_write_time(pb, " begin", pkt->pts);
184  avio_w8(pb, '\n');
185  ttml_write_time(pb, " end", pkt->pts + pkt->duration);
186  avio_printf(pb, ">");
187  avio_write(pb, pkt->data, pkt->size);
188  avio_printf(pb, "</p>\n");
189  break;
191  // dump the given document out as-is.
192  if (ttml_ctx->document_written) {
194  "Attempting to write multiple TTML documents into a "
195  "single document! The XML specification forbids this "
196  "as there has to be a single root tag.\n");
197  return AVERROR(EINVAL);
198  }
199  avio_write(pb, pkt->data, pkt->size);
200  ttml_ctx->document_written = 1;
201  break;
202  default:
204  "Internal error: invalid TTML input packet type: %d!\n",
205  ttml_ctx->input_type);
206  return AVERROR_BUG;
207  }
208 
209  return 0;
210 }
211 
213 {
214  TTMLMuxContext *ttml_ctx = ctx->priv_data;
215  AVIOContext *pb = ctx->pb;
216 
217  if (ttml_ctx->input_type == PACKET_TYPE_PARAGRAPH)
219 
220  return 0;
221 }
222 
224  .name = "ttml",
225  .long_name = NULL_IF_CONFIG_SMALL("TTML subtitle"),
226  .extensions = "ttml",
227  .mime_type = "text/ttml",
228  .priv_data_size = sizeof(TTMLMuxContext),
231  .subtitle_codec = AV_CODEC_ID_TTML,
235 };
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
TTMLHeaderParameters
Definition: ttmlenc.c:41
AVOutputFormat::name
const char * name
Definition: avformat.h:496
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ttml_write_trailer
static int ttml_write_trailer(AVFormatContext *ctx)
Definition: ttmlenc.c:212
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:470
ttml_write_packet
static int ttml_write_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: ttmlenc.c:174
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1175
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
TTMLHeaderParameters::tt_element_params
const char * tt_element_params
Definition: ttmlenc.c:42
TTMLENC_EXTRADATA_SIGNATURE_SIZE
#define TTMLENC_EXTRADATA_SIGNATURE_SIZE
Definition: ttmlenc.h:26
PACKET_TYPE_PARAGRAPH
@ PACKET_TYPE_PARAGRAPH
Definition: ttmlenc.c:37
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
ttml_footer_text
static const char ttml_footer_text[]
Definition: ttmlenc.c:60
ttml_header_text
static const char ttml_header_text[]
Definition: ttmlenc.c:51
av_dict_get
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
ttml_write_time
static void ttml_write_time(AVIOContext *pb, const char tag[], int64_t millisec)
Definition: ttmlenc.c:65
TTMLMuxContext
Definition: ttmlenc.c:46
TTMLPacketType
TTMLPacketType
Definition: ttmlenc.c:36
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVFormatContext
Format I/O context.
Definition: avformat.h:1107
ttml_write_header
static int ttml_write_header(AVFormatContext *ctx)
Definition: ttmlenc.c:122
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:994
TTMLHeaderParameters::pre_body_elements
const char * pre_body_elements
Definition: ttmlenc.c:43
NULL
#define NULL
Definition: coverity.c:32
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:98
av_strnlen
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
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1149
TTMLENC_EXTRADATA_SIGNATURE
#define TTMLENC_EXTRADATA_SIGNATURE
Definition: ttmlenc.h:25
TTMLMuxContext::input_type
enum TTMLPacketType input_type
Definition: ttmlenc.c:47
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:905
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:193
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1163
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
TTMLMuxContext::document_written
unsigned int document_written
Definition: ttmlenc.c:48
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
avpriv_set_pts_info
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:4800
PACKET_TYPE_DOCUMENT
@ PACKET_TYPE_DOCUMENT
Definition: ttmlenc.c:38
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:215
ttml_set_header_values_from_extradata
static int ttml_set_header_values_from_extradata(AVCodecParameters *par, struct TTMLHeaderParameters *header_params)
Definition: ttmlenc.c:80
write_packet
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:729
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:466
AVOutputFormat
Definition: avformat.h:495
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
internal.h
ttmlenc.h
AV_CODEC_ID_TTML
@ AV_CODEC_ID_TTML
Definition: codec_id.h:544
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:477
tag
uint32_t tag
Definition: movenc.c:1594
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:847
ff_ttml_muxer
const AVOutputFormat ff_ttml_muxer
Definition: ttmlenc.c:223
avformat.h
avio_printf
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
Writes a formatted string to the context.
ttml_default_namespacing
static const char ttml_default_namespacing[]
Definition: ttmlenc.h:28
AVDictionaryEntry
Definition: dict.h:79
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVDictionaryEntry::value
char * value
Definition: dict.h:81
avstring.h
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:347
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1135
min
float min
Definition: vorbis_enc_data.h:429