FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/webvttenc.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 74 93 79.6%
Functions: 14 15 93.3%
Branches: 29 42 69.0%

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 char stack[WEBVTT_STACK_SIZE];
38 int stack_ptr;
39 } WebVTTContext;
40
41 85 static av_printf_format(2, 3) void webvtt_print(WebVTTContext *s, const char *str, ...)
42 {
43 va_list vargs;
44 85 va_start(vargs, str);
45 85 av_vbprintf(&s->buffer, str, vargs);
46 85 va_end(vargs);
47 85 }
48
49 10 static int webvtt_stack_push(WebVTTContext *s, const char c)
50 {
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (s->stack_ptr >= WEBVTT_STACK_SIZE)
52 return -1;
53 10 s->stack[s->stack_ptr++] = c;
54 10 return 0;
55 }
56
57 10 static char webvtt_stack_pop(WebVTTContext *s)
58 {
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (s->stack_ptr <= 0)
60 return 0;
61 10 return s->stack[--s->stack_ptr];
62 }
63
64 11 static int webvtt_stack_find(WebVTTContext *s, const char c)
65 {
66 int i;
67
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 for (i = s->stack_ptr-1; i >= 0; i--)
68
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (s->stack[i] == c)
69 10 break;
70 11 return i;
71 }
72
73 10 static void webvtt_close_tag(WebVTTContext *s, char tag)
74 {
75 10 webvtt_print(s, "</%c>", tag);
76 10 }
77
78 58 static void webvtt_stack_push_pop(WebVTTContext *s, const char c, int close)
79 {
80
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 10 times.
58 if (close) {
81
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 37 times.
48 int i = c ? webvtt_stack_find(s, c) : 0;
82
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 47 times.
48 if (i < 0)
83 1 return;
84
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 47 times.
57 while (s->stack_ptr != i)
85 10 webvtt_close_tag(s, webvtt_stack_pop(s));
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 } else if (webvtt_stack_push(s, c) < 0)
87 av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
88 }
89
90 37 static void webvtt_style_apply(WebVTTContext *s, const char *style)
91 {
92 37 ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
93
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 if (st) {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (st->bold != ASS_DEFAULT_BOLD) {
95 webvtt_print(s, "<b>");
96 webvtt_stack_push(s, 'b');
97 }
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (st->italic != ASS_DEFAULT_ITALIC) {
99 webvtt_print(s, "<i>");
100 webvtt_stack_push(s, 'i');
101 }
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (st->underline != ASS_DEFAULT_UNDERLINE) {
103 webvtt_print(s, "<u>");
104 webvtt_stack_push(s, 'u');
105 }
106 }
107 37 }
108
109 102 static void webvtt_text_cb(void *priv, const char *text, int len)
110 {
111 102 WebVTTContext *s = priv;
112 102 av_bprint_append_data(&s->buffer, text, len);
113 102 }
114
115 65 static void webvtt_new_line_cb(void *priv, int forced)
116 {
117 65 webvtt_print(priv, "\n");
118 65 }
119
120 28 static void webvtt_style_cb(void *priv, char style, int close)
121 {
122
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 if (style == 's') // strikethrough unsupported
123 7 return;
124
125 21 webvtt_stack_push_pop(priv, style, close);
126
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 11 times.
21 if (!close)
127 10 webvtt_print(priv, "<%c>", style);
128 }
129
130 static void webvtt_cancel_overrides_cb(void *priv, const char *style)
131 {
132 webvtt_stack_push_pop(priv, 0, 1);
133 webvtt_style_apply(priv, style);
134 }
135
136 37 static void webvtt_end_cb(void *priv)
137 {
138 37 webvtt_stack_push_pop(priv, 0, 1);
139 37 }
140
141 static const ASSCodesCallbacks webvtt_callbacks = {
142 .text = webvtt_text_cb,
143 .new_line = webvtt_new_line_cb,
144 .style = webvtt_style_cb,
145 .color = NULL,
146 .font_name = NULL,
147 .font_size = NULL,
148 .alignment = NULL,
149 .cancel_overrides = webvtt_cancel_overrides_cb,
150 .move = NULL,
151 .end = webvtt_end_cb,
152 };
153
154 37 static int webvtt_encode_frame(AVCodecContext *avctx,
155 unsigned char *buf, int bufsize, const AVSubtitle *sub)
156 {
157 37 WebVTTContext *s = avctx->priv_data;
158 ASSDialog *dialog;
159 int i;
160
161 37 av_bprint_init_for_buffer(&s->buffer, buf, bufsize);
162
163
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 37 times.
74 for (i=0; i<sub->num_rects; i++) {
164 37 const char *ass = sub->rects[i]->ass;
165
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (sub->rects[i]->type != SUBTITLE_ASS) {
167 av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
168 return AVERROR(EINVAL);
169 }
170
171 37 dialog = ff_ass_split_dialog(s->ass_ctx, ass);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!dialog)
173 return AVERROR(ENOMEM);
174 37 webvtt_style_apply(s, dialog->style);
175 37 ff_ass_split_override_codes(&webvtt_callbacks, s, dialog->text);
176 37 ff_ass_free_dialog(&dialog);
177 }
178
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (!s->buffer.len)
180 return 0;
181
182
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 if (!av_bprint_is_complete(&s->buffer)) {
183 av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
184 return AVERROR_BUFFER_TOO_SMALL;
185 }
186
187 37 return s->buffer.len;
188 }
189
190 1 static int webvtt_encode_close(AVCodecContext *avctx)
191 {
192 1 WebVTTContext *s = avctx->priv_data;
193 1 ff_ass_split_free(s->ass_ctx);
194 1 return 0;
195 }
196
197 1 static av_cold int webvtt_encode_init(AVCodecContext *avctx)
198 {
199 1 WebVTTContext *s = avctx->priv_data;
200 1 s->avctx = avctx;
201 1 s->ass_ctx = ff_ass_split(avctx->subtitle_header);
202
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
203 }
204
205 const FFCodec ff_webvtt_encoder = {
206 .p.name = "webvtt",
207 CODEC_LONG_NAME("WebVTT subtitle"),
208 .p.type = AVMEDIA_TYPE_SUBTITLE,
209 .p.id = AV_CODEC_ID_WEBVTT,
210 .priv_data_size = sizeof(WebVTTContext),
211 .init = webvtt_encode_init,
212 FF_CODEC_ENCODE_SUB_CB(webvtt_encode_frame),
213 .close = webvtt_encode_close,
214 };
215