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 "libavutil/mem.h"
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "isom.h"
26 #include "movenc.h"
27 #include "movenc_ttml.h"
29 
30 static const unsigned char empty_ttml_document[] =
31  "<tt xml:lang=\"\" xmlns=\"http://www.w3.org/ns/ttml\" />";
32 
33 static int mov_init_ttml_writer(MOVTrack *track, AVFormatContext **out_ctx)
34 {
35  AVStream *movenc_stream = track->st, *ttml_stream = NULL;
36  int ret = AVERROR_BUG;
37 
38  if ((ret = avformat_alloc_output_context2(out_ctx, NULL,
39  "ttml", NULL)) < 0)
40  return ret;
41 
42  if ((ret = avio_open_dyn_buf(&(*out_ctx)->pb)) < 0)
43  return ret;
44 
45  if (!(ttml_stream = avformat_new_stream(*out_ctx, NULL))) {
46  return AVERROR(ENOMEM);
47  }
48 
49  if ((ret = avcodec_parameters_copy(ttml_stream->codecpar,
50  movenc_stream->codecpar)) < 0)
51  return ret;
52 
53  ttml_stream->time_base = movenc_stream->time_base;
54 
55  return 0;
56 }
57 
59  AVFormatContext *ttml_ctx,
60  MOVTrack *track,
61  AVPacket *pkt,
62  int64_t *out_start_ts,
63  int64_t *out_duration)
64 {
65  int ret = AVERROR_BUG;
66  int64_t start_ts = track->start_dts == AV_NOPTS_VALUE ?
67  0 : (track->start_dts + track->track_duration);
68  int64_t end_ts = start_ts;
69 
70  if ((ret = avformat_write_header(ttml_ctx, NULL)) < 0) {
71  return ret;
72  }
73 
75  end_ts = FFMAX(end_ts, pkt->pts + pkt->duration);
76 
77  // in case of the 'dfxp' muxing mode, each written document is offset
78  // to its containing sample's beginning.
79  if (track->par->codec_tag == MOV_ISMV_TTML_TAG) {
80  pkt->dts = pkt->pts = (pkt->pts - start_ts);
81  }
82 
83  pkt->stream_index = 0;
84 
86  ttml_ctx->streams[pkt->stream_index]->time_base);
87 
88  if ((ret = av_write_frame(ttml_ctx, pkt)) < 0) {
89  goto cleanup;
90  }
91 
93  }
94 
95  if ((ret = av_write_trailer(ttml_ctx)) < 0)
96  goto cleanup;
97 
98  *out_start_ts = start_ts;
99  *out_duration = end_ts - start_ts;
100 
101  ret = 0;
102 
103 cleanup:
104  return ret;
105 }
106 
108  MOVTrack *track, AVPacket *pkt)
109 {
110  AVFormatContext *ttml_ctx = NULL;
111  // values for the generated AVPacket
112  int64_t start_ts = 0;
113  int64_t duration = 0;
114 
115  int ret = AVERROR_BUG;
116 
117  if ((ret = mov_init_ttml_writer(track, &ttml_ctx)) < 0) {
118  av_log(s, AV_LOG_ERROR, "Failed to initialize the TTML writer: %s\n",
119  av_err2str(ret));
120  goto cleanup;
121  }
122 
123  if (!track->squashed_packet_queue.head) {
124  // empty queue, write minimal empty document with zero duration
125  avio_write(ttml_ctx->pb, empty_ttml_document,
126  sizeof(empty_ttml_document) - 1);
127  start_ts = 0;
128  duration = 0;
129  goto generate_packet;
130  }
131 
132  if ((ret = mov_write_ttml_document_from_queue(s, ttml_ctx, track, pkt,
133  &start_ts,
134  &duration)) < 0) {
136  "Failed to generate a squashed TTML packet from the packet "
137  "queue: %s\n",
138  av_err2str(ret));
139  goto cleanup;
140  }
141 
142 generate_packet:
143  {
144  // Generate an AVPacket from the data written into the dynamic buffer.
145  uint8_t *buf = NULL;
146  int buf_len = avio_close_dyn_buf(ttml_ctx->pb, &buf);
147  ttml_ctx->pb = NULL;
148 
149  if ((ret = av_packet_from_data(pkt, buf, buf_len)) < 0) {
151  "Failed to create a TTML AVPacket from AVIO data: %s\n",
152  av_err2str(ret));
153  av_freep(&buf);
154  goto cleanup;
155  }
156 
157  pkt->pts = pkt->dts = start_ts;
158  pkt->duration = duration;
160  }
161 
162  ret = 0;
163 
164 cleanup:
165  if (ttml_ctx)
166  ffio_free_dyn_buf(&ttml_ctx->pb);
167 
168  avformat_free_context(ttml_ctx);
169  return ret;
170 }
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:427
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:34
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:30
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:542
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:579
MOVTrack
Definition: movenc.h:86
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: packet.c:580
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:58
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:1407
pkt
AVPacket * pkt
Definition: movenc.c:60
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:65
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
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:487
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:33
av_write_frame
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:1233
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:107
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: packet.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:523
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:530
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: packet.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:1295
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:517
avio_internal.h
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1435
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:526
mem.h
packet_internal.h
AVPacket
This structure stores compressed data.
Definition: packet.h:501
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:94
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