FFmpeg coverage


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