Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavformat/sccdec.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SCC subtitle demuxer
3
 * Copyright (c) 2017 Paul B Mahol
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 "avformat.h"
23
#include "demux.h"
24
#include "internal.h"
25
#include "subtitles.h"
26
#include "libavutil/avstring.h"
27
#include "libavutil/intreadwrite.h"
28
29
typedef struct SCCContext {
30
    FFDemuxSubtitlesQueue q;
31
} SCCContext;
32
33
static int scc_probe(const AVProbeData *p)
34
924k
{
35
924k
    char buf[18];
36
924k
    FFTextReader tr;
37
38
924k
    ff_text_init_buf(&tr, p->buf, p->buf_size);
39
40
2.93M
    while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
41
2.00M
        ff_text_r8(&tr);
42
43
924k
    ff_text_read(&tr, buf, sizeof(buf));
44
45
924k
    if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
46
440
        return AVPROBE_SCORE_MAX;
47
48
924k
    return 0;
49
924k
}
50
51
static int convert(uint8_t x)
52
844k
{
53
844k
    if (x >= 'a')
54
179k
        x -= 'a' - 10;
55
665k
    else if (x >= 'A')
56
11.5k
        x -= 'A' - 10;
57
653k
    else
58
653k
        x -= '0';
59
844k
    return x;
60
844k
}
61
62
static int scc_read_header(AVFormatContext *s)
63
6.64k
{
64
6.64k
    SCCContext *scc = s->priv_data;
65
6.64k
    AVStream *st = avformat_new_stream(s, NULL);
66
6.64k
    AVPacket *sub = NULL;
67
6.64k
    ptrdiff_t len;
68
6.64k
    uint8_t out[4096];
69
6.64k
    FFTextReader tr;
70
71
6.64k
    ff_text_init_avio(s, &tr, s->pb);
72
73
6.64k
    if (!st)
74
0
        return AVERROR(ENOMEM);
75
6.64k
    avpriv_set_pts_info(st, 64, 1, 1000);
76
6.64k
    st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
77
6.64k
    st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
78
79
6.20M
    while (1) {
80
6.20M
        char *saveptr = NULL, *lline;
81
6.20M
        int hh, mm, ss, fs, i;
82
6.20M
        char line[4096];
83
6.20M
        int64_t pos, ts;
84
85
6.20M
        pos = ff_text_pos(&tr);
86
6.20M
        len = ff_subtitles_read_line(&tr, line, sizeof(line));
87
6.20M
        if (len <= 13) {
88
5.55M
            if (ff_text_eof(&tr))
89
6.64k
                break;
90
5.54M
            continue;
91
5.55M
        }
92
647k
        if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) != 4)
93
30.8k
            continue;
94
95
616k
        ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL;
96
616k
        if (sub)
97
615k
            sub->duration = ts - sub->pts;
98
99
616k
        lline  = line;
100
616k
        lline += 12;
101
102
828k
        for (i = 0; i < 4095; i += 3) {
103
828k
            char *ptr = av_strtok(lline, " ", &saveptr);
104
828k
            char c1, c2, c3, c4;
105
828k
            uint8_t o1, o2;
106
107
828k
            if (!ptr)
108
41.2k
                break;
109
110
786k
            if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
111
575k
                break;
112
211k
            o1 = convert(c2) | (convert(c1) << 4);
113
211k
            o2 = convert(c4) | (convert(c3) << 4);
114
115
211k
            lline = NULL;
116
117
211k
            if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
118
211k
                (av_strncasecmp(saveptr, "942f", 4) && !av_strncasecmp(saveptr, "942c", 4))) {
119
120
290
                sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
121
290
                if (!sub)
122
0
                    return AVERROR(ENOMEM);
123
124
290
                sub->pos = pos;
125
290
                pos += i;
126
290
                sub->pts = ts;
127
290
                sub->duration = i * 11;
128
290
                ts += sub->duration;
129
290
                i = 0;
130
290
            }
131
132
211k
            out[i+0] = 0xfc;
133
211k
            out[i+1] = o1;
134
211k
            out[i+2] = o2;
135
211k
        }
136
137
616k
        sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
138
616k
        if (!sub)
139
0
            return AVERROR(ENOMEM);
140
141
616k
        sub->pos = pos;
142
616k
        sub->pts = ts;
143
616k
    }
144
145
6.64k
    ff_subtitles_queue_finalize(s, &scc->q);
146
147
6.64k
    return 0;
148
6.64k
}
149
150
const FFInputFormat ff_scc_demuxer = {
151
    .p.name         = "scc",
152
    .p.long_name    = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
153
    .p.extensions   = "scc",
154
    .priv_data_size = sizeof(SCCContext),
155
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
156
    .read_probe     = scc_probe,
157
    .read_header    = scc_read_header,
158
    .read_packet    = ff_subtitles_read_packet,
159
    .read_seek2     = ff_subtitles_read_seek,
160
    .read_close     = ff_subtitles_read_close,
161
};