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 
74  end_ts = FFMAX(end_ts, pkt->pts + pkt->duration);
75 
76  // in case of the 'dfxp' muxing mode, each written document is offset
77  // to its containing sample's beginning.
78  if (track->par->codec_tag == MOV_ISMV_TTML_TAG) {
79  pkt->dts = pkt->pts = (pkt->pts - start_ts);
80  }
81 
82  pkt->stream_index = 0;
83 
85  ttml_ctx->streams[pkt->stream_index]->time_base);
86 
87  if ((ret = av_write_frame(ttml_ctx, pkt)) < 0) {
88  goto cleanup;
89  }
90 
92  }
93 
94  if ((ret = av_write_trailer(ttml_ctx)) < 0)
95  goto cleanup;
96 
97  *out_start_ts = start_ts;
98  *out_duration = end_ts - start_ts;
99 
100  ret = 0;
101 
102 cleanup:
103  return ret;
104 }
105 
107  MOVTrack *track, AVPacket *pkt)
108 {
109  AVFormatContext *ttml_ctx = NULL;
110  // values for the generated AVPacket
111  int64_t start_ts = 0;
112  int64_t duration = 0;
113 
114  int ret = AVERROR_BUG;
115 
116  if ((ret = mov_init_ttml_writer(track, &ttml_ctx)) < 0) {
117  av_log(s, AV_LOG_ERROR, "Failed to initialize the TTML writer: %s\n",
118  av_err2str(ret));
119  goto cleanup;
120  }
121 
122  if (!track->squashed_packet_queue.head) {
123  // empty queue, write minimal empty document with zero duration
124  avio_write(ttml_ctx->pb, empty_ttml_document,
125  sizeof(empty_ttml_document) - 1);
126  start_ts = 0;
127  duration = 0;
128  goto generate_packet;
129  }
130 
131  if ((ret = mov_write_ttml_document_from_queue(s, ttml_ctx, track, pkt,
132  &start_ts,
133  &duration)) < 0) {
135  "Failed to generate a squashed TTML packet from the packet "
136  "queue: %s\n",
137  av_err2str(ret));
138  goto cleanup;
139  }
140 
141 generate_packet:
142  {
143  // Generate an AVPacket from the data written into the dynamic buffer.
144  uint8_t *buf = NULL;
145  int buf_len = avio_close_dyn_buf(ttml_ctx->pb, &buf);
146  ttml_ctx->pb = NULL;
147 
148  if ((ret = av_packet_from_data(pkt, buf, buf_len)) < 0) {
150  "Failed to create a TTML AVPacket from AVIO data: %s\n",
151  av_err2str(ret));
152  av_freep(&buf);
153  goto cleanup;
154  }
155 
156  pkt->pts = pkt->dts = start_ts;
157  pkt->duration = duration;
159  }
160 
161  ret = 0;
162 
163 cleanup:
164  if (ttml_ctx)
165  ffio_free_dyn_buf(&ttml_ctx->pb);
166 
167  avformat_free_context(ttml_ctx);
168  return ret;
169 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:427
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:34
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList *pkt_buffer, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:580
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 struct AVCodec *c)
Add a new stream to a media file.
empty_ttml_document
static const unsigned char empty_ttml_document[]
Definition: movenc_ttml.c:29
int64_t
long long int64_t
Definition: coverity.c:34
cleanup
static av_cold void cleanup(FlashSV2Context *s)
Definition: flashsv2enc.c:130
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1323
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
MOVTrack::track_duration
int64_t track_duration
Definition: movenc.h:91
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
MOVTrack
Definition: movenc.h:86
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:109
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:1406
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
MOVTrack::squashed_packet_queue
PacketList squashed_packet_queue
Definition: movenc.h:172
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:1361
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:486
MOVTrack::start_dts
int64_t start_dts
Definition: movenc.h:122
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
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:782
NULL
#define NULL
Definition: coverity.c:32
isom.h
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1297
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:1232
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:106
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:172
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:521
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:200
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
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:531
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:1294
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
avio_internal.h
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1434
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avformat.h
MOVTrack::par
AVCodecParameters * par
Definition: movenc.h:110
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:141
AVPacket::stream_index
int stream_index
Definition: packet.h:524
packet_internal.h
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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:27
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:93
MOV_ISMV_TTML_TAG
#define MOV_ISMV_TTML_TAG
Definition: isom.h:448
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106