Coverage Report

Created: 2025-11-24 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/codec/webvtt/webvtt.c
Line
Count
Source
1
/*****************************************************************************
2
 * webvtt.c: WEBVTT shared code
3
 *****************************************************************************
4
 * Copyright (C) 2017 VideoLabs, VLC authors and VideoLAN
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program 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
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
21
#ifdef HAVE_CONFIG_H
22
# include "config.h"
23
#endif
24
25
#include <vlc_common.h>
26
#include <vlc_charset.h>
27
#include <vlc_plugin.h>
28
29
#include "webvtt.h"
30
31
#include <ctype.h>
32
#include <assert.h>
33
34
/*****************************************************************************
35
 * Modules descriptor.
36
 *****************************************************************************/
37
38
108
vlc_module_begin ()
39
54
    set_capability( "spu decoder", 10 )
40
54
    set_shortname( N_("WEBVTT decoder"))
41
54
    set_description( N_("WEBVTT subtitles decoder") )
42
108
    set_callbacks( webvtt_OpenDecoder, webvtt_CloseDecoder )
43
54
    set_subcategory( SUBCAT_INPUT_SCODEC )
44
54
    add_submodule()
45
54
        set_shortname( "WEBVTT" )
46
54
        set_description( N_("WEBVTT subtitles parser") )
47
54
        set_capability( "demux", 11 )
48
54
        set_subcategory( SUBCAT_INPUT_DEMUX )
49
54
        set_callbacks( webvtt_OpenDemux, webvtt_CloseDemux )
50
54
        add_shortcut( "webvtt" )
51
54
    add_submodule()
52
54
        set_shortname( "WEBVTT" )
53
54
        set_description( N_("WEBVTT subtitles parser") )
54
54
        set_capability( "demux", 0 )
55
54
        set_subcategory( SUBCAT_INPUT_DEMUX )
56
54
        set_callbacks( webvtt_OpenDemuxStream, webvtt_CloseDemux )
57
54
        add_shortcut( "webvttstream" )
58
54
#ifdef ENABLE_SOUT
59
54
    add_submodule()
60
54
        set_description( "WEBVTT text encoder" )
61
54
        set_capability( "spu encoder", 101 )
62
54
        set_subcategory( SUBCAT_INPUT_SCODEC )
63
54
        set_callback( webvtt_OpenEncoder )
64
54
    add_submodule()
65
54
        set_description( N_("Raw WebVTT muxer") )
66
54
        set_capability( "sout mux", 0 )
67
54
        set_subcategory( SUBCAT_SOUT_MUX )
68
54
        add_shortcut( "webvtt", "rawvtt" )
69
108
        set_callbacks( webvtt_OpenMuxer, webvtt_CloseMuxer )
70
54
#endif
71
54
vlc_module_end ()
72
73
struct webvtt_text_parser_t
74
{
75
    enum
76
    {
77
        WEBVTT_SECTION_UNDEFINED = WEBVTT_HEADER_STYLE - 1,
78
        WEBVTT_SECTION_STYLE = WEBVTT_HEADER_STYLE,
79
        WEBVTT_SECTION_REGION = WEBVTT_HEADER_REGION,
80
        WEBVTT_SECTION_NOTE,
81
        WEBVTT_SECTION_CUES,
82
    } section;
83
    char * reads[3];
84
85
    void * priv;
86
    webvtt_cue_t *(*pf_get_cue)( void * );
87
    void (*pf_cue_done)( void *, webvtt_cue_t * );
88
    void (*pf_header)( void *, enum webvtt_header_line_e, bool, const char * );
89
90
    webvtt_cue_t *p_cue;
91
};
92
93
static vlc_tick_t MakeTime( int t[4] )
94
286k
{
95
286k
    return vlc_tick_from_sec( t[0] * 3600 + t[1] * 60 + t[2] ) +
96
286k
           VLC_TICK_FROM_MS(t[3]);
97
286k
}
98
99
bool webvtt_scan_time( const char *psz, vlc_tick_t *p_time )
100
321k
{
101
321k
    int t[4];
102
321k
    if( sscanf( psz, "%2d:%2d.%3d",
103
321k
                      &t[1], &t[2], &t[3] ) == 3 )
104
49.6k
    {
105
49.6k
        t[0] = 0;
106
49.6k
        *p_time = MakeTime( t );
107
49.6k
        return true;
108
49.6k
    }
109
271k
    else if( sscanf( psz, "%d:%2d:%2d.%3d",
110
271k
                          &t[0], &t[1], &t[2], &t[3] ) == 4 )
111
237k
    {
112
237k
        *p_time = MakeTime( t );
113
237k
        return true;
114
237k
    }
115
34.6k
    else return false;
116
321k
}
117
118
static bool KeywordMatch( const char *psz, const char *keyword )
119
261k
{
120
261k
    const size_t i_len = strlen(keyword);
121
261k
    return( !strncmp( keyword, psz, i_len ) && (!psz[i_len] || isspace(psz[i_len])) );
122
261k
}
123
124
/*
125
126
*/
127
128
webvtt_text_parser_t * webvtt_text_parser_New( void *priv,
129
                    webvtt_cue_t *(*pf_get_cue)( void * ),
130
                    void (*pf_cue_done)( void *, webvtt_cue_t * ),
131
                    void (*pf_header)( void *, enum webvtt_header_line_e, bool, const char * ) )
132
16.9k
{
133
16.9k
    webvtt_text_parser_t *p = malloc(sizeof(*p));
134
16.9k
    if( p )
135
16.9k
    {
136
16.9k
        p->section = WEBVTT_SECTION_UNDEFINED;
137
67.9k
        for( int i=0; i<3; i++ )
138
50.9k
            p->reads[i] = NULL;
139
16.9k
        p->p_cue = NULL;
140
16.9k
        p->priv = priv;
141
16.9k
        p->pf_cue_done = pf_cue_done;
142
16.9k
        p->pf_get_cue = pf_get_cue;
143
16.9k
        p->pf_header = pf_header;
144
16.9k
    }
145
16.9k
    return p;
146
16.9k
}
147
148
void webvtt_text_parser_Delete( webvtt_text_parser_t *p )
149
16.9k
{
150
67.9k
    for( int i=0; i<3; i++ )
151
50.9k
        free( p->reads[i] );
152
16.9k
    free( p );
153
16.9k
}
154
155
static void forward_line( webvtt_text_parser_t *p, const char *psz_line, bool b_new )
156
281k
{
157
281k
    if( p->pf_header )
158
281k
        p->pf_header( p->priv, (enum webvtt_header_line_e)p->section,
159
281k
                      b_new, psz_line );
160
281k
}
161
162
void webvtt_text_parser_Feed( webvtt_text_parser_t *p, char *psz_line )
163
1.91M
{
164
1.91M
    if( psz_line == NULL )
165
8.49k
    {
166
8.49k
        if( p->p_cue )
167
4.29k
        {
168
4.29k
            if( p->pf_cue_done )
169
4.29k
                p->pf_cue_done( p->priv, p->p_cue );
170
4.29k
            p->p_cue = NULL;
171
4.29k
        }
172
8.49k
        return;
173
8.49k
    }
174
175
1.91M
    free(p->reads[0]);
176
1.91M
    p->reads[0] = p->reads[1];
177
1.91M
    p->reads[1] = p->reads[2];
178
1.91M
    p->reads[2] = psz_line;
179
180
    /* Lookup keywords */
181
1.91M
    if( unlikely(p->section == WEBVTT_SECTION_UNDEFINED) )
182
71.4k
    {
183
71.4k
        if( KeywordMatch( psz_line, "\xEF\xBB\xBFWEBVTT" ) ||
184
68.4k
            KeywordMatch( psz_line, "WEBVTT" )  )
185
13.0k
        {
186
13.0k
            p->section = WEBVTT_SECTION_UNDEFINED;
187
13.0k
            if( p->p_cue )
188
0
            {
189
0
                if( p->pf_cue_done )
190
0
                    p->pf_cue_done( p->priv, p->p_cue );
191
0
                p->p_cue = NULL;
192
0
            }
193
13.0k
            return;
194
13.0k
        }
195
58.4k
        else if( KeywordMatch( psz_line, "STYLE" ) )
196
22.5k
        {
197
22.5k
            p->section = WEBVTT_SECTION_STYLE;
198
22.5k
            forward_line( p, psz_line, true );
199
22.5k
            return;
200
22.5k
        }
201
35.8k
        else if( KeywordMatch( psz_line, "REGION" ) )
202
8.24k
        {
203
8.24k
            p->section = WEBVTT_SECTION_REGION;
204
8.24k
            forward_line( p, psz_line, true );
205
8.24k
            return;
206
8.24k
        }
207
27.6k
        else if( KeywordMatch( psz_line, "NOTE" ) )
208
217
        {
209
217
            p->section = WEBVTT_SECTION_NOTE;
210
217
            return;
211
217
        }
212
27.4k
        else if( psz_line[0] != 0 )
213
6.10k
        {
214
6.10k
            p->section = WEBVTT_SECTION_CUES;
215
6.10k
        }
216
71.4k
    }
217
218
1.86M
    if( likely(p->section == WEBVTT_SECTION_CUES) )
219
1.59M
    {
220
1.59M
        if( p->p_cue )
221
213k
        {
222
213k
            if( psz_line[0] == 0 )
223
107k
            {
224
107k
                if( p->p_cue )
225
107k
                {
226
107k
                    if( p->pf_cue_done )
227
107k
                        p->pf_cue_done( p->priv, p->p_cue );
228
107k
                    p->p_cue = NULL;
229
107k
                }
230
107k
            }
231
105k
            else
232
105k
            {
233
105k
                char *psz_merged;
234
105k
                if( -1 < asprintf( &psz_merged, "%s\n%s", p->p_cue->psz_text, psz_line ) )
235
105k
                {
236
105k
                    free( p->p_cue->psz_text );
237
105k
                    p->p_cue->psz_text = psz_merged;
238
105k
                }
239
105k
                return;
240
105k
            }
241
213k
        }
242
243
1.48M
        if( p->reads[1] == NULL )
244
46.9k
            return;
245
246
1.44M
        const char *psz_split = strstr( p->reads[1], " --> " );
247
1.44M
        if( psz_split )
248
125k
        {
249
125k
            vlc_tick_t i_start, i_stop;
250
251
125k
            if( webvtt_scan_time( p->reads[1], &i_start ) &&
252
120k
                webvtt_scan_time( psz_split + 5,  &i_stop ) && i_start <= i_stop )
253
112k
            {
254
112k
                const char *psz_attrs = strchr( psz_split + 5 + 5, ' ' );
255
112k
                p->p_cue = ( p->pf_get_cue ) ? p->pf_get_cue( p->priv ) : NULL;
256
112k
                if( p->p_cue )
257
112k
                {
258
112k
                    p->p_cue->psz_attrs = ( psz_attrs ) ? strdup( psz_attrs ) : NULL;
259
112k
                    p->p_cue->psz_id = p->reads[0];
260
112k
                    p->reads[0] = NULL;
261
112k
                    p->p_cue->psz_text = p->reads[2];
262
112k
                    p->reads[2] = NULL;
263
112k
                    p->p_cue->i_start = i_start;
264
112k
                    p->p_cue->i_stop = i_stop;
265
112k
                }
266
112k
            }
267
125k
        }
268
1.44M
    }
269
272k
    else if( p->section == WEBVTT_SECTION_STYLE )
270
199k
    {
271
199k
        forward_line( p, psz_line, false );
272
199k
        if( psz_line[0] == 0 )
273
19.1k
            p->section = WEBVTT_SECTION_UNDEFINED;
274
199k
    }
275
73.7k
    else if( p->section == WEBVTT_SECTION_REGION )
276
51.9k
    {
277
51.9k
        forward_line( p, psz_line, false );
278
51.9k
        if( psz_line[0] == 0 ) /* End of region declaration */
279
7.00k
            p->section = WEBVTT_SECTION_UNDEFINED;
280
51.9k
    }
281
21.8k
    else if( p->section == WEBVTT_SECTION_NOTE )
282
546
    {
283
546
        if( psz_line[0] == 0 )
284
208
            p->section = WEBVTT_SECTION_UNDEFINED;
285
546
    }
286
1.86M
}