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
25 #include "subtitles.h"
26 #include "libavutil/avstring.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
);
71 ff_text_init_avio(s
, &tr
, s
->pb
);
74 return AVERROR(ENOMEM
);
75 avpriv_set_pts_info(st
, 64, 1, 1000);
76 st
->codecpar
->codec_type
= AVMEDIA_TYPE_SUBTITLE
;
77 st
->codecpar
->codec_id
= AV_CODEC_ID_EIA_608
;
80 char *saveptr
= NULL
, *lline
;
81 int hh
, mm
, ss
, fs
, i
;
85 pos
= ff_text_pos(&tr
);
86 len
= ff_subtitles_read_line(&tr
, line
, sizeof(line
));
92 if (av_sscanf(line
, "%d:%d:%d%*[:;]%d", &hh
, &mm
, &ss
, &fs
) != 4)
95 ts
= (hh
* 3600LL + mm
* 60LL + ss
) * 1000LL + fs
* 33LL;
97 sub
->duration
= ts
- sub
->pts
;
102 for (i
= 0; i
< 4095; i
+= 3) {
103 char *ptr
= av_strtok(lline
, " ", &saveptr
);
110 if (av_sscanf(ptr
, "%c%c%c%c", &c1
, &c2
, &c3
, &c4
) != 4)
112 o1
= convert(c2
) | (convert(c1
) << 4);
113 o2
= convert(c4
) | (convert(c3
) << 4);
117 if (i
> 12 && o1
== 0x94 && o2
== 0x20 && saveptr
&&
118 (av_strncasecmp(saveptr
, "942f", 4) && !av_strncasecmp(saveptr
, "942c", 4))) {
120 sub
= ff_subtitles_queue_insert(&scc
->q
, out
, i
, 0);
122 return AVERROR(ENOMEM
);
127 sub
->duration
= i
* 11;
137 sub
= ff_subtitles_queue_insert(&scc
->q
, out
, i
, 0);
139 return AVERROR(ENOMEM
);
145 ff_subtitles_queue_finalize(s
, &scc
->q
);
150 const FFInputFormat ff_scc_demuxer
= {
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
,