3 * Copyright (c) 2017 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "subtitles.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/intreadwrite.h"
28 typedef struct SCCContext {
29 FFDemuxSubtitlesQueue q;
32 static int scc_probe(const AVProbeData *p)
37 ff_text_init_buf(&tr, p->buf, p->buf_size);
39 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
42 ff_text_read(&tr, buf, sizeof(buf));
44 if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
45 return AVPROBE_SCORE_MAX;
50 static int convert(uint8_t x)
61 static int scc_read_header(AVFormatContext *s)
63 SCCContext *scc = s->priv_data;
64 AVStream *st = avformat_new_stream(s, NULL);
65 char line[4096], line2[4096];
66 int64_t ts_start, ts_end;
67 int count = 0, ret = 0;
72 ff_text_init_avio(s, &tr, s->pb);
75 return AVERROR(ENOMEM);
76 avpriv_set_pts_info(st, 64, 1, 1000);
77 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
78 st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
80 while (!ff_text_eof(&tr)) {
81 int64_t current_pos, next_pos;
82 char *saveptr = NULL, *lline;
83 int hh1, mm1, ss1, fs1, i;
84 int hh2, mm2, ss2, fs2;
88 current_pos = ff_text_pos(&tr);
89 while (!ff_text_eof(&tr)) {
90 len = ff_subtitles_read_line(&tr, line, sizeof(line));
96 if (!strncmp(line, "Scenarist_SCC V1.0", 18))
98 if (sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
101 ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
103 next_pos = ff_text_pos(&tr);
104 while (!ff_text_eof(&tr)) {
105 len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
109 if (sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
112 ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
116 lline = (char *)&line;
119 for (i = 0; i < 4095; i += 3) {
120 char *ptr = av_strtok(lline, " ", &saveptr);
126 if (sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
131 out[i+1] = convert(c2) | (convert(c1) << 4);
132 out[i+2] = convert(c4) | (convert(c3) << 4);
136 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
138 return AVERROR(ENOMEM);
140 sub->pos = current_pos;
142 sub->duration = ts_end - ts_start;
143 memmove(line, line2, sizeof(line));
144 current_pos = next_pos;
154 ff_subtitles_queue_finalize(s, &scc->q);
159 static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
161 SCCContext *scc = s->priv_data;
162 return ff_subtitles_queue_read_packet(&scc->q, pkt);
165 static int scc_read_seek(AVFormatContext *s, int stream_index,
166 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
168 SCCContext *scc = s->priv_data;
169 return ff_subtitles_queue_seek(&scc->q, s, stream_index,
170 min_ts, ts, max_ts, flags);
173 static int scc_read_close(AVFormatContext *s)
175 SCCContext *scc = s->priv_data;
176 ff_subtitles_queue_clean(&scc->q);
180 AVInputFormat ff_scc_demuxer = {
182 .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
183 .priv_data_size = sizeof(SCCContext),
184 .read_probe = scc_probe,
185 .read_header = scc_read_header,
186 .read_packet = scc_read_packet,
187 .read_seek2 = scc_read_seek,
188 .read_close = scc_read_close,