Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/webvttenc.c
Line
Count
Source (jump to first uncovered line)
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
0
#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
static av_printf_format(2, 3) void webvtt_print(WebVTTContext *s, const char *str, ...)
42
0
{
43
0
    va_list vargs;
44
0
    va_start(vargs, str);
45
0
    av_vbprintf(&s->buffer, str, vargs);
46
0
    va_end(vargs);
47
0
}
48
49
static int webvtt_stack_push(WebVTTContext *s, const char c)
50
0
{
51
0
    if (s->stack_ptr >= WEBVTT_STACK_SIZE)
52
0
        return -1;
53
0
    s->stack[s->stack_ptr++] = c;
54
0
    return 0;
55
0
}
56
57
static char webvtt_stack_pop(WebVTTContext *s)
58
0
{
59
0
    if (s->stack_ptr <= 0)
60
0
        return 0;
61
0
    return s->stack[--s->stack_ptr];
62
0
}
63
64
static int webvtt_stack_find(WebVTTContext *s, const char c)
65
0
{
66
0
    int i;
67
0
    for (i = s->stack_ptr-1; i >= 0; i--)
68
0
        if (s->stack[i] == c)
69
0
            break;
70
0
    return i;
71
0
}
72
73
static void webvtt_close_tag(WebVTTContext *s, char tag)
74
0
{
75
0
    webvtt_print(s, "</%c>", tag);
76
0
}
77
78
static void webvtt_stack_push_pop(WebVTTContext *s, const char c, int close)
79
0
{
80
0
    if (close) {
81
0
        int i = c ? webvtt_stack_find(s, c) : 0;
82
0
        if (i < 0)
83
0
            return;
84
0
        while (s->stack_ptr != i)
85
0
            webvtt_close_tag(s, webvtt_stack_pop(s));
86
0
    } else if (webvtt_stack_push(s, c) < 0)
87
0
        av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
88
0
}
89
90
static void webvtt_style_apply(WebVTTContext *s, const char *style)
91
0
{
92
0
    ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
93
0
    if (st) {
94
0
        if (st->bold != ASS_DEFAULT_BOLD) {
95
0
            webvtt_print(s, "<b>");
96
0
            webvtt_stack_push(s, 'b');
97
0
        }
98
0
        if (st->italic != ASS_DEFAULT_ITALIC) {
99
0
            webvtt_print(s, "<i>");
100
0
            webvtt_stack_push(s, 'i');
101
0
        }
102
0
        if (st->underline != ASS_DEFAULT_UNDERLINE) {
103
0
            webvtt_print(s, "<u>");
104
0
            webvtt_stack_push(s, 'u');
105
0
        }
106
0
    }
107
0
}
108
109
static void webvtt_text_cb(void *priv, const char *text, int len)
110
0
{
111
0
    WebVTTContext *s = priv;
112
0
    av_bprint_append_data(&s->buffer, text, len);
113
0
}
114
115
static void webvtt_new_line_cb(void *priv, int forced)
116
0
{
117
0
    webvtt_print(priv, "\n");
118
0
}
119
120
static void webvtt_style_cb(void *priv, char style, int close)
121
0
{
122
0
    if (style == 's') // strikethrough unsupported
123
0
        return;
124
125
0
    webvtt_stack_push_pop(priv, style, close);
126
0
    if (!close)
127
0
        webvtt_print(priv, "<%c>", style);
128
0
}
129
130
static void webvtt_cancel_overrides_cb(void *priv, const char *style)
131
0
{
132
0
    webvtt_stack_push_pop(priv, 0, 1);
133
0
    webvtt_style_apply(priv, style);
134
0
}
135
136
static void webvtt_end_cb(void *priv)
137
0
{
138
0
    webvtt_stack_push_pop(priv, 0, 1);
139
0
}
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
static int webvtt_encode_frame(AVCodecContext *avctx,
155
                               unsigned char *buf, int bufsize, const AVSubtitle *sub)
156
0
{
157
0
    WebVTTContext *s = avctx->priv_data;
158
0
    ASSDialog *dialog;
159
0
    int i;
160
161
0
    av_bprint_init_for_buffer(&s->buffer, buf, bufsize);
162
163
0
    for (i=0; i<sub->num_rects; i++) {
164
0
        const char *ass = sub->rects[i]->ass;
165
166
0
        if (sub->rects[i]->type != SUBTITLE_ASS) {
167
0
            av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
168
0
            return AVERROR(EINVAL);
169
0
        }
170
171
0
        dialog = ff_ass_split_dialog(s->ass_ctx, ass);
172
0
        if (!dialog)
173
0
            return AVERROR(ENOMEM);
174
0
        webvtt_style_apply(s, dialog->style);
175
0
        ff_ass_split_override_codes(&webvtt_callbacks, s, dialog->text);
176
0
        ff_ass_free_dialog(&dialog);
177
0
    }
178
179
0
    if (!s->buffer.len)
180
0
        return 0;
181
182
0
    if (!av_bprint_is_complete(&s->buffer)) {
183
0
        av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
184
0
        return AVERROR_BUFFER_TOO_SMALL;
185
0
    }
186
187
0
    return s->buffer.len;
188
0
}
189
190
static int webvtt_encode_close(AVCodecContext *avctx)
191
0
{
192
0
    WebVTTContext *s = avctx->priv_data;
193
0
    ff_ass_split_free(s->ass_ctx);
194
0
    return 0;
195
0
}
196
197
static av_cold int webvtt_encode_init(AVCodecContext *avctx)
198
0
{
199
0
    WebVTTContext *s = avctx->priv_data;
200
0
    s->avctx = avctx;
201
0
    s->ass_ctx = ff_ass_split(avctx->subtitle_header);
202
0
    return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
203
0
}
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
};