FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/webvttenc.c
Date: 2026-09-28 04:46:15
Exec Total Coverage
Lines: 109 130 83.8%
Functions: 17 18 94.4%
Branches: 39 58 67.2%

Line Branch Exec Source
1 /*
2 * WebVTT subtitle encoder
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 * Copyright (c) 2014 Aman Gupta <ffmpeg@tmm1.net>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdarg.h>
24 #include "avcodec.h"
25 #include "libavutil/bprint.h"
26 #include "ass_split.h"
27 #include "ass.h"
28 #include "codec_internal.h"
29
30 #define WEBVTT_STACK_SIZE 64
31 typedef struct {
32 AVCodecContext *avctx;
33 ASSSplitContext *ass_ctx;
34 AVBPrint buffer;
35 unsigned timestamp_end;
36 int count;
37 int64_t cue_start_ms;
38 int64_t cue_end_ms;
39 int64_t current_time_ms;
40 int64_t last_timestamp_ms;
41 int64_t pending_duration_ms;
42 char stack[WEBVTT_STACK_SIZE];
43 int stack_ptr;
44 } WebVTTContext;
45
46 109 static av_printf_format(2, 3) void webvtt_print(WebVTTContext *s, const char *str, ...)
47 {
48 va_list vargs;
49 109 va_start(vargs, str);
50 109 av_vbprintf(&s->buffer, str, vargs);
51 109 va_end(vargs);
52 109 }
53
54 14 static int webvtt_stack_push(WebVTTContext *s, const char c)
55 {
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (s->stack_ptr >= WEBVTT_STACK_SIZE)
57 ✗ return -1;
58 14 s->stack[s->stack_ptr++] = c;
59 14 return 0;
60 }
61
62 14 static char webvtt_stack_pop(WebVTTContext *s)
63 {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (s->stack_ptr <= 0)
65 ✗ return 0;
66 14 return s->stack[--s->stack_ptr];
67 }
68
69 15 static int webvtt_stack_find(WebVTTContext *s, const char c)
70 {
71 int i;
72
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1 times.
15 for (i = s->stack_ptr-1; i >= 0; i--)
73
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (s->stack[i] == c)
74 14 break;
75 15 return i;
76 }
77
78 14 static void webvtt_close_tag(WebVTTContext *s, char tag)
79 {
80 14 webvtt_print(s, "</%c>", tag);
81 14 }
82
83 71 static void webvtt_stack_push_pop(WebVTTContext *s, const char c, int close)
84 {
85
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 14 times.
71 if (close) {
86
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 42 times.
57 int i = c ? webvtt_stack_find(s, c) : 0;
87
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 56 times.
57 if (i < 0)
88 1 return;
89
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 56 times.
70 while (s->stack_ptr != i)
90 14 webvtt_close_tag(s, webvtt_stack_pop(s));
91
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 } else if (webvtt_stack_push(s, c) < 0)
92 ✗ av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
93 }
94
95 42 static void webvtt_style_apply(WebVTTContext *s, const char *style)
96 {
97 42 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
98
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (st) {
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (st->bold != ASS_DEFAULT_BOLD) {
100 ✗ webvtt_print(s, "<b>");
101 ✗ webvtt_stack_push(s, 'b');
102 }
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (st->italic != ASS_DEFAULT_ITALIC) {
104 ✗ webvtt_print(s, "<i>");
105 ✗ webvtt_stack_push(s, 'i');
106 }
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (st->underline != ASS_DEFAULT_UNDERLINE) {
108 ✗ webvtt_print(s, "<u>");
109 ✗ webvtt_stack_push(s, 'u');
110 }
111 }
112 42 }
113
114 15 static void webvtt_print_timestamp(WebVTTContext *s, int64_t millisec)
115 {
116 15 int64_t sec = millisec / 1000;
117 15 millisec %= 1000;
118 15 int64_t min = sec / 60;
119 15 sec %= 60;
120 15 int64_t hour = min / 60;
121 15 min %= 60;
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (hour > 0) {
123 ✗ webvtt_print(s, "<%02"PRId64":%02"PRId64":%02"PRId64".%03"PRId64">", hour, min, sec, millisec);
124 } else {
125 15 webvtt_print(s, "<%02"PRId64":%02"PRId64".%03"PRId64">", min, sec, millisec);
126 }
127 15 }
128
129 58 static void webvtt_flush_karaoke_timestamp(WebVTTContext *s)
130 {
131
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
58 if (s->pending_duration_ms <= 0)
132 42 return;
133
134 32 int64_t next_ts = s->current_time_ms <= INT64_MAX - s->pending_duration_ms ?
135
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 s->current_time_ms + s->pending_duration_ms : INT64_MAX;
136
3/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 1 times.
16 if (next_ts > s->cue_start_ms && next_ts < s->cue_end_ms &&
137
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 next_ts > s->last_timestamp_ms) {
138 15 webvtt_print_timestamp(s, next_ts);
139 15 s->last_timestamp_ms = next_ts;
140 }
141 16 s->current_time_ms = next_ts;
142 16 s->pending_duration_ms = 0;
143 }
144
145 115 static void webvtt_text_cb(void *priv, const char *text, int len)
146 {
147 115 WebVTTContext *s = priv;
148 115 av_bprint_append_data(&s->buffer, text, len);
149 115 }
150
151 66 static void webvtt_new_line_cb(void *priv, int forced)
152 {
153 66 WebVTTContext *s = priv;
154 66 webvtt_print(s, "\n");
155 66 }
156
157 16 static void webvtt_karaoke_cb(void *priv, unsigned int duration)
158 {
159 16 WebVTTContext *s = priv;
160
161 16 webvtt_flush_karaoke_timestamp(s);
162 16 s->pending_duration_ms = (int64_t)duration * 10;
163 16 }
164
165 36 static void webvtt_style_cb(void *priv, char style, int close)
166 {
167
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 29 times.
36 if (style == 's') // strikethrough unsupported
168 7 return;
169
170 29 webvtt_stack_push_pop(priv, style, close);
171
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 15 times.
29 if (!close)
172 14 webvtt_print(priv, "<%c>", style);
173 }
174
175 ✗ static void webvtt_cancel_overrides_cb(void *priv, const char *style)
176 {
177 ✗ webvtt_stack_push_pop(priv, 0, 1);
178 ✗ webvtt_style_apply(priv, style);
179 ✗ }
180
181 42 static void webvtt_end_cb(void *priv)
182 {
183 42 WebVTTContext *s = priv;
184 42 webvtt_flush_karaoke_timestamp(s);
185 42 webvtt_stack_push_pop(priv, 0, 1);
186 42 }
187
188 static const ASSCodesCallbacks webvtt_callbacks = {
189 .text = webvtt_text_cb,
190 .new_line = webvtt_new_line_cb,
191 .style = webvtt_style_cb,
192 .color = NULL,
193 .font_name = NULL,
194 .font_size = NULL,
195 .alignment = NULL,
196 .cancel_overrides = webvtt_cancel_overrides_cb,
197 .move = NULL,
198 .karaoke = webvtt_karaoke_cb,
199 .end = webvtt_end_cb,
200 };
201
202 42 static int webvtt_encode_frame(AVCodecContext *avctx,
203 unsigned char *buf, int bufsize, const AVSubtitle *sub)
204 {
205 42 WebVTTContext *s = avctx->priv_data;
206 ASSDialog *dialog;
207 int i;
208
209 42 av_bprint_init_for_buffer(&s->buffer, buf, bufsize);
210
211 /* Multiple rectangles are independent overlays. One monotonic WebVTT
212 * cue cannot represent them, so keep the previous plain-text path. */
213
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
42 if (sub->pts != AV_NOPTS_VALUE && sub->num_rects == 1) {
214 42 s->cue_start_ms = av_rescale_q(sub->pts, AV_TIME_BASE_Q, (AVRational){ 1, 1000 });
215 42 s->cue_end_ms = s->cue_start_ms + sub->end_display_time;
216 } else {
217 ✗ s->cue_start_ms = s->cue_end_ms = 0;
218 }
219 42 s->current_time_ms = s->cue_start_ms;
220 42 s->last_timestamp_ms = -1;
221 42 s->pending_duration_ms = 0;
222
223
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
84 for (i=0; i<sub->num_rects; i++) {
224 42 const char *ass = sub->rects[i]->ass;
225
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (sub->rects[i]->type != SUBTITLE_ASS) {
227 ✗ av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
228 ✗ return AVERROR(EINVAL);
229 }
230
231 42 dialog = ff_ass_split_dialog(s->ass_ctx, ass);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!dialog)
233 ✗ return AVERROR(ENOMEM);
234 42 webvtt_style_apply(s, dialog->style);
235 42 ff_ass_split_override_codes(&webvtt_callbacks, s, dialog->text);
236 42 ff_ass_free_dialog(&dialog);
237 }
238
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!s->buffer.len)
240 ✗ return 0;
241
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if (!av_bprint_is_complete(&s->buffer)) {
243 ✗ av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
244 ✗ return AVERROR_BUFFER_TOO_SMALL;
245 }
246
247 42 return s->buffer.len;
248 }
249
250 3 static av_cold int webvtt_encode_close(AVCodecContext *avctx)
251 {
252 3 WebVTTContext *s = avctx->priv_data;
253 3 ff_ass_split_free(s->ass_ctx);
254 3 return 0;
255 }
256
257 3 static av_cold int webvtt_encode_init(AVCodecContext *avctx)
258 {
259 3 WebVTTContext *s = avctx->priv_data;
260 3 s->avctx = avctx;
261 3 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
262
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
263 }
264
265 const FFCodec ff_webvtt_encoder = {
266 .p.name = "webvtt",
267 CODEC_LONG_NAME("WebVTT subtitle"),
268 .p.type = AVMEDIA_TYPE_SUBTITLE,
269 .p.id = AV_CODEC_ID_WEBVTT,
270 .priv_data_size = sizeof(WebVTTContext),
271 .init = webvtt_encode_init,
272 FF_CODEC_ENCODE_SUB_CB(webvtt_encode_frame),
273 .close = webvtt_encode_close,
274 };
275