FFmpeg
movenc_ttml.c
Go to the documentation of this file.
1 /*
2  * MP4, ISMV Muxer TTML helpers
3  * Copyright (c) 2021 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 #include "avformat.h"
23 #include "avio_internal.h"
24 #include "isom.h"
25 #include "movenc.h"
26 #include "movenc_ttml.h"
28 
29 static const unsigned char empty_ttml_document[] =
30  "<tt xml:lang=\"\" xmlns=\"http://www.w3.org/ns/ttml\" />";
31 
32 static int mov_init_ttml_writer(MOVTrack *track, AVFormatContext **out_ctx)
33 {
34  AVStream *movenc_stream = track->st, *ttml_stream = NULL;
35  int ret = AVERROR_BUG;
36 
37  if ((ret = avformat_alloc_output_context2(out_ctx, NULL,
38  "ttml", NULL)) < 0)
39  return ret;
40 
41  if ((ret = avio_open_dyn_buf(&(*out_ctx)->pb)) < 0)
42  return ret;
43 
44  if (!(ttml_stream = avformat_new_stream(*out_ctx, NULL))) {
45  return AVERROR(ENOMEM);
46  }
47 
48  if ((ret = avcodec_parameters_copy(ttml_stream->codecpar,
49  movenc_stream->codecpar)) < 0)
50  return ret;
51 
52  ttml_stream->time_base = movenc_stream->time_base;
53 
54  return 0;
55 }
56 
58  AVFormatContext *ttml_ctx,
59  MOVTrack *track,
60  AVPacket *pkt,
61  int64_t *out_start_ts,
62  int64_t *out_duration)
63 {
64  int ret = AVERROR_BUG;
65  int64_t start_ts = track->start_dts == AV_NOPTS_VALUE ?
66  0 : (track->start_dts + track->track_duration);
67  int64_t end_ts = start_ts;
68 
69  if ((ret = avformat_write_header(ttml_ctx, NULL)) < 0) {
70  return ret;
71  }
72 
75  pkt)) {
76  end_ts = FFMAX(end_ts, pkt->pts + pkt->duration);
77 
78  // in case of the 'dfxp' muxing mode, each written document is offset
79  // to its containing sample's beginning.
80  if (track->par->codec_tag == MOV_ISMV_TTML_TAG) {
81  pkt->dts = pkt->pts = (pkt->pts - start_ts);
82  }
83 
84  pkt->stream_index = 0;
85 
87  ttml_ctx->streams[pkt->stream_index]->time_base);
88 
89  if ((ret = av_write_frame(ttml_ctx, pkt)) < 0) {
90  goto cleanup;
91  }
92 
94  }
95 
96  if ((ret = av_write_trailer(ttml_ctx)) < 0)
97  goto cleanup;
98 
99  *out_start_ts = start_ts;
100  *out_duration = end_ts - start_ts;
101 
102  ret = 0;
103 
104 cleanup:
105  return ret;
106 }
107 
109  MOVTrack *track, AVPacket *pkt)
110 {
111  AVFormatContext *ttml_ctx = NULL;
112  // values for the generated AVPacket
113  int64_t start_ts = 0;
114  int64_t duration = 0;
115 
116  int ret = AVERROR_BUG;
117 
118  if ((ret = mov_init_ttml_writer(track, &ttml_ctx)) < 0) {
119  av_log(s, AV_LOG_ERROR, "Failed to initialize the TTML writer: %s\n",
120  av_err2str(ret));
121  goto cleanup;
122  }
123 
124  if (!track->squashed_packet_queue) {
125  // empty queue, write minimal empty document with zero duration
126  avio_write(ttml_ctx->pb, empty_ttml_document,
127  sizeof(empty_ttml_document) - 1);
128  start_ts = 0;
129  duration = 0;
130  goto generate_packet;
131  }
132 
133  if ((ret = mov_write_ttml_document_from_queue(s, ttml_ctx, track, pkt,
134  &start_ts,
135  &duration)) < 0) {
137  "Failed to generate a squashed TTML packet from the packet "
138  "queue: %s\n",
139  av_err2str(ret));
140  goto cleanup;
141  }
142 
143 generate_packet:
144  {
145  // Generate an AVPacket from the data written into the dynamic buffer.
146  uint8_t *buf = NULL;
147  int buf_len = avio_close_dyn_buf(ttml_ctx->pb, &buf);
148  ttml_ctx->pb = NULL;
149 
150  if ((ret = av_packet_from_data(pkt, buf, buf_len)) < 0) {
152  "Failed to create a TTML AVPacket from AVIO data: %s\n",
153  av_err2str(ret));
154  av_freep(&buf);
155  goto cleanup;
156  }
157 
158  pkt->pts = pkt->dts = start_ts;
159  pkt->duration = duration;
161  }
162 
163  ret = 0;
164 
165 cleanup:
166  if (ttml_ctx)
167  ffio_free_dyn_buf(&ttml_ctx->pb);
168 
169  avformat_free_context(ttml_ctx);
170  return ret;
171 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:417
movenc_ttml.h
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:724
empty_ttml_document
static const unsigned char empty_ttml_document[]
Definition: movenc_ttml.c:29
MOVTrack::squashed_packet_queue_end
PacketList * squashed_packet_queue_end
Definition: movenc.h:170
cleanup
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:128
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1177
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MOVTrack::track_duration
int64_t track_duration
Definition: movenc.h:90
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:425
MOVTrack
Definition: movenc.h:85
mov_write_ttml_document_from_queue
static int mov_write_ttml_document_from_queue(AVFormatContext *s, AVFormatContext *ttml_ctx, MOVTrack *track, AVPacket *pkt, int64_t *out_start_ts, int64_t *out_duration)
Definition: movenc_ttml.c:57
MOVTrack::st
AVStream * st
Definition: movenc.h:108
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1470
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
duration
int64_t duration
Definition: movenc.c:64
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1425
s
#define s(width, name)
Definition: cbs_vp9.c:257
MOVTrack::squashed_packet_queue
PacketList * squashed_packet_queue
Definition: movenc.h:170
avformat_write_header
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:467
MOVTrack::start_dts
int64_t start_dts
Definition: movenc.h:120
AVFormatContext
Format I/O context.
Definition: avformat.h:1109
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1004
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:880
NULL
#define NULL
Definition: coverity.c:32
isom.h
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1151
mov_init_ttml_writer
static int mov_init_ttml_writer(MOVTrack *track, AVFormatContext **out_ctx)
Definition: movenc_ttml.c:32
av_write_frame
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1188
movenc.h
ff_mov_generate_squashed_ttml_packet
int ff_mov_generate_squashed_ttml_packet(AVFormatContext *s, MOVTrack *track, AVPacket *pkt)
Definition: movenc_ttml.c:108
av_packet_from_data
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:166
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
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
av_packet_rescale_ts
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another.
Definition: avpacket.c:521
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:72
av_write_trailer
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1250
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
avio_internal.h
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1498
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:857
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList **pkt_buffer, PacketList **pkt_buffer_end, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:571
avformat.h
MOVTrack::par
AVCodecParameters * par
Definition: movenc.h:109
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:679
AVPacket::stream_index
int stream_index
Definition: packet.h:375
packet_internal.h
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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
avformat_alloc_output_context2
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:136
MOV_ISMV_TTML_TAG
#define MOV_ISMV_TTML_TAG
Definition: isom.h:392