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"
27#include "libavcodec/packet_internal.h"
28
29static const unsigned char empty_ttml_document[] =
30    "<tt xml:lang=\"\" xmlns=\"http://www.w3.org/ns/ttml\" />";
31
32static 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
57static int mov_write_ttml_document_from_queue(AVFormatContext *s,
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
73    while (!avpriv_packet_list_get(&track->squashed_packet_queue, pkt)) {
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
84        av_packet_rescale_ts(pkt, track->st->time_base,
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
91        av_packet_unref(pkt);
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
102cleanup:
103    return ret;
104}
105
106int ff_mov_generate_squashed_ttml_packet(AVFormatContext *s,
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) {
134        av_log(s, AV_LOG_ERROR,
135               "Failed to generate a squashed TTML packet from the packet "
136               "queue: %s\n",
137               av_err2str(ret));
138        goto cleanup;
139    }
140
141generate_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) {
149            av_log(s, AV_LOG_ERROR,
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;
158        pkt->flags |= AV_PKT_FLAG_KEY;
159    }
160
161    ret = 0;
162
163cleanup:
164    if (ttml_ctx)
165        ffio_free_dyn_buf(&ttml_ctx->pb);
166
167    avformat_free_context(ttml_ctx);
168    return ret;
169}
170