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/avstring.h"
26 #include "libavutil/bprint.h"
27 #include "libavutil/intreadwrite.h"
29 typedef struct SCCContext {
30 FFDemuxSubtitlesQueue q;
33 static int scc_probe(const AVProbeData *p)
38 ff_text_init_buf(&tr, p->buf, p->buf_size);
40 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43 ff_text_read(&tr, buf, sizeof(buf));
45 if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
46 return AVPROBE_SCORE_MAX;
51 static int convert(uint8_t x)
62 static int scc_read_header(AVFormatContext *s)
64 SCCContext *scc = s->priv_data;
65 AVStream *st = avformat_new_stream(s, NULL);
66 char line2[4096], line[4096];
67 int64_t pos, ts, next_ts = AV_NOPTS_VALUE;
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) || next_ts == AV_NOPTS_VALUE || line2[0]) {
81 char *saveptr = NULL, *lline;
82 int hh, mm, ss, fs, i;
85 if (next_ts == AV_NOPTS_VALUE) {
86 while (!ff_text_eof(&tr)) {
87 len = ff_subtitles_read_line(&tr, line, sizeof(line));
90 if (!strncmp(line, "Scenarist_SCC V1.0", 18))
92 if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
96 ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL;
98 while (!ff_text_eof(&tr)) {
99 len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
103 if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
107 memmove(line, line2, sizeof(line));
110 while (!ff_text_eof(&tr)) {
111 len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
115 if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
120 next_ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33LL;
122 pos = ff_text_pos(&tr);
123 lline = (char *)&line;
126 for (i = 0; i < 4095; i += 3) {
127 char *ptr = av_strtok(lline, " ", &saveptr);
134 if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
136 o1 = convert(c2) | (convert(c1) << 4);
137 o2 = convert(c4) | (convert(c3) << 4);
141 if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
142 (av_strncasecmp(saveptr, "942f", 4) && !av_strncasecmp(saveptr, "942c", 4))) {
146 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
153 sub->duration = i * 11;
165 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
171 sub->duration = next_ts - ts;
175 ff_subtitles_queue_finalize(s, &scc->q);
179 ff_subtitles_queue_clean(&scc->q);
180 return AVERROR(ENOMEM);
183 static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
185 SCCContext *scc = s->priv_data;
186 return ff_subtitles_queue_read_packet(&scc->q, pkt);
189 static int scc_read_seek(AVFormatContext *s, int stream_index,
190 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
192 SCCContext *scc = s->priv_data;
193 return ff_subtitles_queue_seek(&scc->q, s, stream_index,
194 min_ts, ts, max_ts, flags);
197 static int scc_read_close(AVFormatContext *s)
199 SCCContext *scc = s->priv_data;
200 ff_subtitles_queue_clean(&scc->q);
204 AVInputFormat ff_scc_demuxer = {
206 .long_name = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
207 .priv_data_size = sizeof(SCCContext),
208 .read_probe = scc_probe,
209 .read_header = scc_read_header,
210 .read_packet = scc_read_packet,
211 .read_seek2 = scc_read_seek,
212 .read_close = scc_read_close,