Coverage Report

Created: 2026-05-16 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/jacosubdec.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2012 Clément Bœsch
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * JACOsub subtitle decoder
24
 * @see http://unicorn.us.com/jacosub/jscripts.html
25
 */
26
27
#include <time.h>
28
#include "ass.h"
29
#include "codec_internal.h"
30
#include "jacosub.h"
31
#include "libavutil/avstring.h"
32
#include "libavutil/bprint.h"
33
#include "libavutil/time_internal.h"
34
35
static int insert_text(AVBPrint *dst, const char *in, const char *arg)
36
2.42k
{
37
2.42k
    av_bprintf(dst, "%s", arg);
38
2.42k
    return 0;
39
2.42k
}
40
41
static int insert_datetime(AVBPrint *dst, const char *in, const char *arg)
42
3.54M
{
43
3.54M
    char buf[16] = {0};
44
3.54M
    time_t now = time(0);
45
3.54M
    struct tm ltime;
46
47
3.54M
    localtime_r(&now, &ltime);
48
3.54M
    if (strftime(buf, sizeof(buf), arg, &ltime))
49
3.54M
        av_bprintf(dst, "%s", buf);
50
3.54M
    return 0;
51
3.54M
}
52
53
static int insert_color(AVBPrint *dst, const char *in, const char *arg)
54
244
{
55
244
    return 1; // skip id
56
244
}
57
58
static int insert_font(AVBPrint *dst, const char *in, const char *arg)
59
424
{
60
424
    return 1; // skip id
61
424
}
62
63
static const struct {
64
    const char *from;
65
    const char *arg;
66
    int (*func)(AVBPrint *dst, const char *in, const char *arg);
67
} ass_codes_map[] = {
68
    {"\\~", "~",        insert_text},       // tilde doesn't need escaping
69
    {"~",   "{\\h}",    insert_text},       // hard space
70
    {"\\n", "\\N",      insert_text},       // newline
71
    {"\\D", "%d %b %Y", insert_datetime},   // current date
72
    {"\\T", "%H:%M",    insert_datetime},   // current time
73
    {"\\N", "{\\r}",    insert_text},       // reset to default style
74
    {"\\I", "{\\i1}",   insert_text},       // italic on
75
    {"\\i", "{\\i0}",   insert_text},       // italic off
76
    {"\\B", "{\\b1}",   insert_text},       // bold on
77
    {"\\b", "{\\b0}",   insert_text},       // bold off
78
    {"\\U", "{\\u1}",   insert_text},       // underline on
79
    {"\\u", "{\\u0}",   insert_text},       // underline off
80
    {"\\C", "",         insert_color},      // TODO: color
81
    {"\\F", "",         insert_font},       // TODO: font
82
};
83
84
enum {
85
    ALIGN_VB = 1<<0, // vertical bottom, default
86
    ALIGN_VM = 1<<1, // vertical middle
87
    ALIGN_VT = 1<<2, // vertical top
88
    ALIGN_JC = 1<<3, // justify center, default
89
    ALIGN_JL = 1<<4, // justify left
90
    ALIGN_JR = 1<<5, // justify right
91
};
92
93
static void jacosub_to_ass(AVCodecContext *avctx, AVBPrint *dst, const char *src)
94
7.97k
{
95
7.97k
    int i, valign = 0, halign = 0;
96
7.97k
    char c = av_toupper(*src);
97
7.97k
    char directives[128] = {0};
98
99
    /* extract the optional directives */
100
7.97k
    if ((c >= 'A' && c <= 'Z') || c == '[') {
101
3.94k
        char *p    = directives;
102
3.94k
        char *pend = directives + sizeof(directives) - 1;
103
104
19.0k
        do *p++ = av_toupper(*src++);
105
19.0k
        while (*src && !jss_whitespace(*src) && p < pend);
106
3.94k
        *p = 0;
107
3.94k
        src = jss_skip_whitespace(src);
108
3.94k
    }
109
110
    /* handle directives (TODO: handle more of them, and more reliably) */
111
7.97k
    if      (strstr(directives, "VB")) valign = ALIGN_VB;
112
7.74k
    else if (strstr(directives, "VM")) valign = ALIGN_VM;
113
6.95k
    else if (strstr(directives, "VT")) valign = ALIGN_VT;
114
7.97k
    if      (strstr(directives, "JC")) halign = ALIGN_JC;
115
7.70k
    else if (strstr(directives, "JL")) halign = ALIGN_JL;
116
6.92k
    else if (strstr(directives, "JR")) halign = ALIGN_JR;
117
7.97k
    if (valign || halign) {
118
2.61k
        if (!valign) valign = ALIGN_VB;
119
2.61k
        if (!halign) halign = ALIGN_JC;
120
2.61k
        switch (valign | halign) {
121
266
        case ALIGN_VB | ALIGN_JL: av_bprintf(dst, "{\\an1}"); break; // bottom left
122
484
        case ALIGN_VB | ALIGN_JC: av_bprintf(dst, "{\\an2}"); break; // bottom center
123
218
        case ALIGN_VB | ALIGN_JR: av_bprintf(dst, "{\\an3}"); break; // bottom right
124
271
        case ALIGN_VM | ALIGN_JL: av_bprintf(dst, "{\\an4}"); break; // middle left
125
309
        case ALIGN_VM | ALIGN_JC: av_bprintf(dst, "{\\an5}"); break; // middle center
126
202
        case ALIGN_VM | ALIGN_JR: av_bprintf(dst, "{\\an6}"); break; // middle right
127
238
        case ALIGN_VT | ALIGN_JL: av_bprintf(dst, "{\\an7}"); break; // top left
128
346
        case ALIGN_VT | ALIGN_JC: av_bprintf(dst, "{\\an8}"); break; // top center
129
281
        case ALIGN_VT | ALIGN_JR: av_bprintf(dst, "{\\an9}"); break; // top right
130
2.61k
        }
131
2.61k
    }
132
133
    /* process timed line */
134
3.60M
    while (*src && *src != '\n') {
135
136
        /* text continue on the next line */
137
3.59M
        if (src[0] == '\\' && src[1] == '\n') {
138
572
            src += 2;
139
1.42k
            while (jss_whitespace(*src))
140
848
                src++;
141
572
            continue;
142
572
        }
143
144
        /* special character codes */
145
14.9M
        for (i = 0; i < FF_ARRAY_ELEMS(ass_codes_map); i++) {
146
14.8M
            const char *from = ass_codes_map[i].from;
147
14.8M
            const char *arg  = ass_codes_map[i].arg;
148
14.8M
            size_t codemap_len = strlen(from);
149
150
14.8M
            if (!strncmp(src, from, codemap_len)) {
151
3.54M
                src += codemap_len;
152
3.54M
                src += ass_codes_map[i].func(dst, src, arg);
153
3.54M
                break;
154
3.54M
            }
155
14.8M
        }
156
157
        /* simple char copy */
158
3.59M
        if (i == FF_ARRAY_ELEMS(ass_codes_map))
159
49.6k
            av_bprintf(dst, "%c", *src++);
160
3.59M
    }
161
7.97k
}
162
163
static int jacosub_decode_frame(AVCodecContext *avctx, AVSubtitle *sub,
164
                                int *got_sub_ptr, const AVPacket *avpkt)
165
182k
{
166
182k
    int ret;
167
182k
    const char *ptr = avpkt->data;
168
182k
    FFASSDecoderContext *s = avctx->priv_data;
169
170
182k
    if (avpkt->size <= 0)
171
0
        goto end;
172
173
182k
    if (*ptr) {
174
90.4k
        AVBPrint buffer;
175
176
        // skip timers
177
90.4k
        ptr = jss_skip_whitespace(ptr);
178
90.4k
        ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
179
8.87k
        ptr = strchr(ptr, ' '); if (!ptr) goto end; ptr++;
180
181
7.97k
        av_bprint_init(&buffer, JSS_MAX_LINESIZE, JSS_MAX_LINESIZE);
182
7.97k
        jacosub_to_ass(avctx, &buffer, ptr);
183
7.97k
        ret = ff_ass_add_rect(sub, buffer.str, s->readorder++, 0, NULL, NULL);
184
7.97k
        av_bprint_finalize(&buffer, NULL);
185
7.97k
        if (ret < 0)
186
0
            return ret;
187
7.97k
    }
188
189
182k
end:
190
182k
    *got_sub_ptr = sub->num_rects > 0;
191
182k
    return avpkt->size;
192
182k
}
193
194
const FFCodec ff_jacosub_decoder = {
195
    .p.name         = "jacosub",
196
    CODEC_LONG_NAME("JACOsub subtitle"),
197
    .p.type         = AVMEDIA_TYPE_SUBTITLE,
198
    .p.id           = AV_CODEC_ID_JACOSUB,
199
    .init           = ff_ass_subtitle_header_default,
200
    FF_CODEC_DECODE_SUB_CB(jacosub_decode_frame),
201
    .flush          = ff_ass_decoder_flush,
202
    .priv_data_size = sizeof(FFASSDecoderContext),
203
};