FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/movenc_ttml.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 53 74 71.6%
Functions: 3 3 100.0%
Branches: 17 30 56.7%

Line Branch Exec Source
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"
28 #include "libavcodec/packet_internal.h"
29
30 static const unsigned char empty_ttml_document[] =
31 "<tt xml:lang=\"\" xmlns=\"http://www.w3.org/ns/ttml\" />";
32
33 2 static int mov_init_ttml_writer(MOVTrack *track, AVFormatContext **out_ctx)
34 {
35 2 AVStream *movenc_stream = track->st, *ttml_stream = NULL;
36 2 int ret = AVERROR_BUG;
37
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avformat_alloc_output_context2(out_ctx, NULL,
39 "ttml", NULL)) < 0)
40 return ret;
41
42
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avio_open_dyn_buf(&(*out_ctx)->pb)) < 0)
43 return ret;
44
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!(ttml_stream = avformat_new_stream(*out_ctx, NULL))) {
46 return AVERROR(ENOMEM);
47 }
48
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((ret = avcodec_parameters_copy(ttml_stream->codecpar,
50 2 movenc_stream->codecpar)) < 0)
51 return ret;
52
53 2 ttml_stream->time_base = movenc_stream->time_base;
54
55 2 return 0;
56 }
57
58 2 static int mov_write_ttml_document_from_queue(AVFormatContext *s,
59 AVFormatContext *ttml_ctx,
60 MOVTrack *track,
61 AVPacket *pkt,
62 int64_t *out_start_ts,
63 int64_t *out_duration)
64 {
65 2 int ret = AVERROR_BUG;
66 4 int64_t start_ts = track->start_dts == AV_NOPTS_VALUE ?
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 0 : (track->start_dts + track->track_duration);
68 2 int64_t end_ts = start_ts;
69
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avformat_write_header(ttml_ctx, NULL)) < 0) {
71 return ret;
72 }
73
74
2/2
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 2 times.
76 while (!avpriv_packet_list_get(&track->squashed_packet_queue, pkt)) {
75 74 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
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 37 times.
74 if (track->par->codec_tag == MOV_ISMV_TTML_TAG) {
80 37 pkt->dts = pkt->pts = (pkt->pts - start_ts);
81 }
82
83 74 pkt->stream_index = 0;
84
85 74 av_packet_rescale_ts(pkt, track->st->time_base,
86 74 ttml_ctx->streams[pkt->stream_index]->time_base);
87
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
74 if ((ret = av_write_frame(ttml_ctx, pkt)) < 0) {
89 goto cleanup;
90 }
91
92 74 av_packet_unref(pkt);
93 }
94
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_write_trailer(ttml_ctx)) < 0)
96 goto cleanup;
97
98 2 *out_start_ts = start_ts;
99 2 *out_duration = end_ts - start_ts;
100
101 2 ret = 0;
102
103 2 cleanup:
104 2 return ret;
105 }
106
107 2 int ff_mov_generate_squashed_ttml_packet(AVFormatContext *s,
108 MOVTrack *track, AVPacket *pkt)
109 {
110 2 AVFormatContext *ttml_ctx = NULL;
111 // values for the generated AVPacket
112 2 int64_t start_ts = 0;
113 2 int64_t duration = 0;
114
115 2 int ret = AVERROR_BUG;
116
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = mov_write_ttml_document_from_queue(s, ttml_ctx, track, pkt,
133 &start_ts,
134 &duration)) < 0) {
135 av_log(s, AV_LOG_ERROR,
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 2 generate_packet:
143 {
144 // Generate an AVPacket from the data written into the dynamic buffer.
145 2 uint8_t *buf = NULL;
146 2 int buf_len = avio_close_dyn_buf(ttml_ctx->pb, &buf);
147 2 ttml_ctx->pb = NULL;
148
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_packet_from_data(pkt, buf, buf_len)) < 0) {
150 av_log(s, AV_LOG_ERROR,
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 2 pkt->pts = pkt->dts = start_ts;
158 2 pkt->duration = duration;
159 2 pkt->flags |= AV_PKT_FLAG_KEY;
160 }
161
162 2 ret = 0;
163
164 2 cleanup:
165
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ttml_ctx)
166 2 ffio_free_dyn_buf(&ttml_ctx->pb);
167
168 2 avformat_free_context(ttml_ctx);
169 2 return ret;
170 }
171