Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/srtenc.c
Line
Count
Source
1
/*
2
 * SubRip subtitle encoder
3
 * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
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 "config_components.h"
23
24
#include <stdarg.h>
25
#include "avcodec.h"
26
#include "libavutil/bprint.h"
27
#include "ass_split.h"
28
#include "ass.h"
29
#include "codec_internal.h"
30
31
32
0
#define SRT_STACK_SIZE 64
33
34
typedef struct {
35
    AVCodecContext *avctx;
36
    ASSSplitContext *ass_ctx;
37
    AVBPrint buffer;
38
    char stack[SRT_STACK_SIZE];
39
    int stack_ptr;
40
    int alignment_applied;
41
} SRTContext;
42
43
44
static av_printf_format(2, 3) void srt_print(SRTContext *s, const char *str, ...)
45
0
{
46
0
    va_list vargs;
47
0
    va_start(vargs, str);
48
0
    av_vbprintf(&s->buffer, str, vargs);
49
0
    va_end(vargs);
50
0
}
51
52
static int srt_stack_push(SRTContext *s, const char c)
53
0
{
54
0
    if (s->stack_ptr >= SRT_STACK_SIZE)
55
0
        return -1;
56
0
    s->stack[s->stack_ptr++] = c;
57
0
    return 0;
58
0
}
59
60
static char srt_stack_pop(SRTContext *s)
61
0
{
62
0
    if (s->stack_ptr <= 0)
63
0
        return 0;
64
0
    return s->stack[--s->stack_ptr];
65
0
}
66
67
static int srt_stack_find(SRTContext *s, const char c)
68
0
{
69
0
    int i;
70
0
    for (i = s->stack_ptr-1; i >= 0; i--)
71
0
        if (s->stack[i] == c)
72
0
            break;
73
0
    return i;
74
0
}
75
76
static void srt_close_tag(SRTContext *s, char tag)
77
0
{
78
0
    srt_print(s, "</%c%s>", tag, tag == 'f' ? "ont" : "");
79
0
}
80
81
static void srt_stack_push_pop(SRTContext *s, const char c, int close)
82
0
{
83
0
    if (close) {
84
0
        int i = c ? srt_stack_find(s, c) : 0;
85
0
        if (i < 0)
86
0
            return;
87
0
        while (s->stack_ptr != i)
88
0
            srt_close_tag(s, srt_stack_pop(s));
89
0
    } else if (srt_stack_push(s, c) < 0)
90
0
        av_log(s->avctx, AV_LOG_ERROR, "tag stack overflow\n");
91
0
}
92
93
static void srt_style_apply(SRTContext *s, const char *style)
94
0
{
95
0
    ASSStyle *st = ff_ass_style_get(s->ass_ctx, style);
96
0
    if (st) {
97
0
        int c = st->primary_color & 0xFFFFFF;
98
0
        if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT) ||
99
0
            st->font_size != ASS_DEFAULT_FONT_SIZE ||
100
0
            c != ASS_DEFAULT_COLOR) {
101
0
            srt_print(s, "<font");
102
0
            if (st->font_name && strcmp(st->font_name, ASS_DEFAULT_FONT))
103
0
                srt_print(s, " face=\"%s\"", st->font_name);
104
0
            if (st->font_size != ASS_DEFAULT_FONT_SIZE)
105
0
                srt_print(s, " size=\"%d\"", st->font_size);
106
0
            if (c != ASS_DEFAULT_COLOR)
107
0
                srt_print(s, " color=\"#%06x\"",
108
0
                          (c & 0xFF0000) >> 16 | c & 0xFF00 | (c & 0xFF) << 16);
109
0
            srt_print(s, ">");
110
0
            srt_stack_push(s, 'f');
111
0
        }
112
0
        if (st->bold != ASS_DEFAULT_BOLD) {
113
0
            srt_print(s, "<b>");
114
0
            srt_stack_push(s, 'b');
115
0
        }
116
0
        if (st->italic != ASS_DEFAULT_ITALIC) {
117
0
            srt_print(s, "<i>");
118
0
            srt_stack_push(s, 'i');
119
0
        }
120
0
        if (st->underline != ASS_DEFAULT_UNDERLINE) {
121
0
            srt_print(s, "<u>");
122
0
            srt_stack_push(s, 'u');
123
0
        }
124
0
        if (st->alignment != ASS_DEFAULT_ALIGNMENT) {
125
0
            srt_print(s, "{\\an%d}", st->alignment);
126
0
            s->alignment_applied = 1;
127
0
        }
128
0
    }
129
0
}
130
131
132
static av_cold int srt_encode_init(AVCodecContext *avctx)
133
0
{
134
0
    SRTContext *s = avctx->priv_data;
135
0
    s->avctx = avctx;
136
0
    s->ass_ctx = ff_ass_split(avctx->subtitle_header);
137
0
    return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
138
0
}
139
140
static void srt_text_cb(void *priv, const char *text, int len)
141
0
{
142
0
    SRTContext *s = priv;
143
0
    av_bprint_append_data(&s->buffer, text, len);
144
0
}
145
146
static void srt_new_line_cb(void *priv, int forced)
147
0
{
148
0
    srt_print(priv, "\n");
149
0
}
150
151
static void srt_style_cb(void *priv, char style, int close)
152
0
{
153
0
    srt_stack_push_pop(priv, style, close);
154
0
    if (!close)
155
0
        srt_print(priv, "<%c>", style);
156
0
}
157
158
static void srt_color_cb(void *priv, unsigned int color, unsigned int color_id)
159
0
{
160
0
    if (color_id > 1)
161
0
        return;
162
0
    srt_stack_push_pop(priv, 'f', color == 0xFFFFFFFF);
163
0
    if (color != 0xFFFFFFFF)
164
0
        srt_print(priv, "<font color=\"#%06x\">",
165
0
              (color & 0xFF0000) >> 16 | color & 0xFF00 | (color & 0xFF) << 16);
166
0
}
167
168
static void srt_font_name_cb(void *priv, const char *name)
169
0
{
170
0
    srt_stack_push_pop(priv, 'f', !name);
171
0
    if (name)
172
0
        srt_print(priv, "<font face=\"%s\">", name);
173
0
}
174
175
static void srt_font_size_cb(void *priv, int size)
176
0
{
177
0
    srt_stack_push_pop(priv, 'f', size < 0);
178
0
    if (size >= 0)
179
0
        srt_print(priv, "<font size=\"%d\">", size);
180
0
}
181
182
static void srt_alignment_cb(void *priv, int alignment)
183
0
{
184
0
    SRTContext *s = priv;
185
0
    if (!s->alignment_applied && alignment >= 0) {
186
0
        srt_print(s, "{\\an%d}", alignment);
187
0
        s->alignment_applied = 1;
188
0
    }
189
0
}
190
191
static void srt_cancel_overrides_cb(void *priv, const char *style)
192
0
{
193
0
    srt_stack_push_pop(priv, 0, 1);
194
0
    srt_style_apply(priv, style);
195
0
}
196
197
static void srt_move_cb(void *priv, int x1, int y1, int x2, int y2,
198
                        int t1, int t2)
199
0
{
200
    // TODO: add a AV_PKT_DATA_SUBTITLE_POSITION side data when a new subtitles
201
    // encoding API passing the AVPacket is available.
202
0
}
203
204
static void srt_end_cb(void *priv)
205
0
{
206
0
    srt_stack_push_pop(priv, 0, 1);
207
0
}
208
209
static const ASSCodesCallbacks srt_callbacks = {
210
    .text             = srt_text_cb,
211
    .new_line         = srt_new_line_cb,
212
    .style            = srt_style_cb,
213
    .color            = srt_color_cb,
214
    .font_name        = srt_font_name_cb,
215
    .font_size        = srt_font_size_cb,
216
    .alignment        = srt_alignment_cb,
217
    .cancel_overrides = srt_cancel_overrides_cb,
218
    .move             = srt_move_cb,
219
    .end              = srt_end_cb,
220
};
221
222
static const ASSCodesCallbacks text_callbacks = {
223
    .text             = srt_text_cb,
224
    .new_line         = srt_new_line_cb,
225
};
226
227
static int encode_frame(AVCodecContext *avctx,
228
                        unsigned char *buf, int bufsize, const AVSubtitle *sub,
229
                        const ASSCodesCallbacks *cb)
230
0
{
231
0
    SRTContext *s = avctx->priv_data;
232
0
    ASSDialog *dialog;
233
0
    int i;
234
235
0
    av_bprint_init_for_buffer(&s->buffer, buf, bufsize);
236
237
0
    for (i=0; i<sub->num_rects; i++) {
238
0
        const char *ass = sub->rects[i]->ass;
239
240
0
        if (sub->rects[i]->type != SUBTITLE_ASS) {
241
0
            av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
242
0
            return AVERROR(EINVAL);
243
0
        }
244
245
0
        dialog = ff_ass_split_dialog(s->ass_ctx, ass);
246
0
        if (!dialog)
247
0
            return AVERROR(ENOMEM);
248
0
        s->alignment_applied = 0;
249
0
        if (avctx->codec_id == AV_CODEC_ID_SUBRIP)
250
0
            srt_style_apply(s, dialog->style);
251
0
        ff_ass_split_override_codes(cb, s, dialog->text);
252
0
        ff_ass_free_dialog(&dialog);
253
0
    }
254
255
0
    if (!s->buffer.len)
256
0
        return 0;
257
258
0
    if (!av_bprint_is_complete(&s->buffer)) {
259
0
        av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
260
0
        return AVERROR_BUFFER_TOO_SMALL;
261
0
    }
262
263
0
    return s->buffer.len;
264
0
}
265
266
static int srt_encode_frame(AVCodecContext *avctx,
267
                               unsigned char *buf, int bufsize, const AVSubtitle *sub)
268
0
{
269
0
    return encode_frame(avctx, buf, bufsize, sub, &srt_callbacks);
270
0
}
271
272
static int text_encode_frame(AVCodecContext *avctx,
273
                             unsigned char *buf, int bufsize, const AVSubtitle *sub)
274
0
{
275
0
    return encode_frame(avctx, buf, bufsize, sub, &text_callbacks);
276
0
}
277
278
static av_cold int srt_encode_close(AVCodecContext *avctx)
279
0
{
280
0
    SRTContext *s = avctx->priv_data;
281
0
    ff_ass_split_free(s->ass_ctx);
282
0
    return 0;
283
0
}
284
285
#if CONFIG_SRT_ENCODER
286
/* deprecated encoder */
287
const FFCodec ff_srt_encoder = {
288
    .p.name         = "srt",
289
    CODEC_LONG_NAME("SubRip subtitle"),
290
    .p.type         = AVMEDIA_TYPE_SUBTITLE,
291
    .p.id           = AV_CODEC_ID_SUBRIP,
292
    .priv_data_size = sizeof(SRTContext),
293
    .init           = srt_encode_init,
294
    FF_CODEC_ENCODE_SUB_CB(srt_encode_frame),
295
    .close          = srt_encode_close,
296
};
297
#endif
298
299
#if CONFIG_SUBRIP_ENCODER
300
const FFCodec ff_subrip_encoder = {
301
    .p.name         = "subrip",
302
    CODEC_LONG_NAME("SubRip subtitle"),
303
    .p.type         = AVMEDIA_TYPE_SUBTITLE,
304
    .p.id           = AV_CODEC_ID_SUBRIP,
305
    .priv_data_size = sizeof(SRTContext),
306
    .init           = srt_encode_init,
307
    FF_CODEC_ENCODE_SUB_CB(srt_encode_frame),
308
    .close          = srt_encode_close,
309
};
310
#endif
311
312
#if CONFIG_TEXT_ENCODER
313
const FFCodec ff_text_encoder = {
314
    .p.name         = "text",
315
    CODEC_LONG_NAME("Raw text subtitle"),
316
    .p.type         = AVMEDIA_TYPE_SUBTITLE,
317
    .p.id           = AV_CODEC_ID_TEXT,
318
    .priv_data_size = sizeof(SRTContext),
319
    .init           = srt_encode_init,
320
    FF_CODEC_ENCODE_SUB_CB(text_encode_frame),
321
    .close          = srt_encode_close,
322
};
323
#endif