]> git.sesse.net Git - ffmpeg/blob - libavformat/sccdec.c
avformat/sccdec: split line with multiple subs
[ffmpeg] / libavformat / sccdec.c
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 "internal.h"
24 #include "subtitles.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/bprint.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 {
35     char buf[18];
36     FFTextReader tr;
37
38     ff_text_init_buf(&tr, p->buf, p->buf_size);
39
40     while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
41         ff_text_r8(&tr);
42
43     ff_text_read(&tr, buf, sizeof(buf));
44
45     if (!memcmp(buf, "Scenarist_SCC V1.0", 18))
46         return AVPROBE_SCORE_MAX;
47
48     return 0;
49 }
50
51 static int convert(uint8_t x)
52 {
53     if (x >= 'a')
54         x -= 87;
55     else if (x >= 'A')
56         x -= 55;
57     else
58         x -= '0';
59     return x;
60 }
61
62 static int scc_read_header(AVFormatContext *s)
63 {
64     SCCContext *scc = s->priv_data;
65     AVStream *st = avformat_new_stream(s, NULL);
66     char line[4096], line2[4096];
67     int64_t ts_start, ts_end;
68     int count = 0, ret = 0;
69     ptrdiff_t len2, len;
70     uint8_t out[4096];
71     FFTextReader tr;
72
73     ff_text_init_avio(s, &tr, s->pb);
74
75     if (!st)
76         return AVERROR(ENOMEM);
77     avpriv_set_pts_info(st, 64, 1, 1000);
78     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
79     st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
80
81     while (!ff_text_eof(&tr)) {
82         int64_t current_pos, next_pos;
83         char *saveptr = NULL, *lline;
84         int hh1, mm1, ss1, fs1, i;
85         int hh2, mm2, ss2, fs2;
86         AVPacket *sub;
87
88         if (count == 0) {
89             current_pos = ff_text_pos(&tr);
90             while (!ff_text_eof(&tr)) {
91                 len = ff_subtitles_read_line(&tr, line, sizeof(line));
92                 if (len > 13)
93                     break;
94             }
95         }
96
97         if (!strncmp(line, "Scenarist_SCC V1.0", 18))
98             continue;
99         if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh1, &mm1, &ss1, &fs1) != 4)
100             continue;
101
102         ts_start = (hh1 * 3600LL + mm1 * 60LL + ss1) * 1000LL + fs1 * 33;
103
104         next_pos = ff_text_pos(&tr);
105         while (!ff_text_eof(&tr)) {
106             len2 = ff_subtitles_read_line(&tr, line2, sizeof(line2));
107             if (len2 > 13)
108                 break;
109         }
110         if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh2, &mm2, &ss2, &fs2) != 4)
111             continue;
112
113         ts_end = (hh2 * 3600LL + mm2 * 60LL + ss2) * 1000LL + fs2 * 33;
114         count++;
115
116 try_again:
117         lline = (char *)&line;
118         lline += 12;
119
120         for (i = 0; i < 4095; i += 3) {
121             char *ptr = av_strtok(lline, " ", &saveptr);
122             char c1, c2, c3, c4;
123             uint8_t o1, o2;
124
125             if (!ptr)
126                 break;
127
128             if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
129                 break;
130             o1 = convert(c2) | (convert(c1) << 4);
131             o2 = convert(c4) | (convert(c3) << 4);
132
133             lline = NULL;
134             out[i+0] = 0xfc;
135             out[i+1] = o1;
136             out[i+2] = o2;
137
138             if (o1 == 0x94 && o2 == 0x2f && saveptr && av_strcasecmp(saveptr, "942f")) {
139                 int64_t duration;
140
141                 i += 3;
142                 out[i] = 0;
143                 duration = i * 11;
144
145                 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
146                 if (!sub)
147                     return AVERROR(ENOMEM);
148
149                 current_pos += i;
150                 sub->pos = current_pos;
151                 sub->pts = ts_start;
152                 sub->duration = duration;
153                 ts_start += duration;
154                 i = 0;
155             }
156         }
157
158         out[i] = 0;
159
160         sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
161         if (!sub)
162             return AVERROR(ENOMEM);
163
164         sub->pos = current_pos;
165         sub->pts = ts_start;
166         sub->duration = ts_end - ts_start;
167         memmove(line, line2, sizeof(line));
168         current_pos = next_pos;
169         line2[0] = 0;
170     }
171
172     if (line[0]) {
173         ts_start = ts_end;
174         ts_end += 1200;
175         goto try_again;
176     }
177
178     ff_subtitles_queue_finalize(s, &scc->q);
179
180     return ret;
181 }
182
183 static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
184 {
185     SCCContext *scc = s->priv_data;
186     return ff_subtitles_queue_read_packet(&scc->q, pkt);
187 }
188
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)
191 {
192     SCCContext *scc = s->priv_data;
193     return ff_subtitles_queue_seek(&scc->q, s, stream_index,
194                                    min_ts, ts, max_ts, flags);
195 }
196
197 static int scc_read_close(AVFormatContext *s)
198 {
199     SCCContext *scc = s->priv_data;
200     ff_subtitles_queue_clean(&scc->q);
201     return 0;
202 }
203
204 AVInputFormat ff_scc_demuxer = {
205     .name           = "scc",
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,
213     .extensions     = "scc",
214 };