]> git.sesse.net Git - ffmpeg/blob - libavformat/sccdec.c
avformat/sccdec: simplify demuxer a little
[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 line2[4096], line[4096];
67     int64_t pos, ts, next_ts = AV_NOPTS_VALUE;
68     int ret = 0;
69     ptrdiff_t 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) || next_ts == AV_NOPTS_VALUE || line2[0]) {
82         char *saveptr = NULL, *lline;
83         int hh, mm, ss, fs, i;
84         AVPacket *sub;
85
86         if (next_ts == AV_NOPTS_VALUE) {
87             while (!ff_text_eof(&tr)) {
88                 len = ff_subtitles_read_line(&tr, line, sizeof(line));
89                 if (len <= 13)
90                     continue;
91                 if (!strncmp(line, "Scenarist_SCC V1.0", 18))
92                     continue;
93                 if (av_sscanf(line, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
94                     break;
95             }
96
97             ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33;
98
99             while (!ff_text_eof(&tr)) {
100                 len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
101                 if (len <= 13)
102                     continue;
103
104                 if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
105                     break;
106             }
107         } else {
108             memmove(line, line2, sizeof(line));
109             line2[0] = 0;
110
111             while (!ff_text_eof(&tr)) {
112                 len = ff_subtitles_read_line(&tr, line2, sizeof(line2));
113                 if (len <= 13)
114                     continue;
115
116                 if (av_sscanf(line2, "%d:%d:%d%*[:;]%d", &hh, &mm, &ss, &fs) == 4)
117                     break;
118             }
119         }
120
121         next_ts = (hh * 3600LL + mm * 60LL + ss) * 1000LL + fs * 33;
122
123         pos = ff_text_pos(&tr);
124         lline = (char *)&line;
125         lline += 12;
126
127         for (i = 0; i < 4095; i += 3) {
128             char *ptr = av_strtok(lline, " ", &saveptr);
129             char c1, c2, c3, c4;
130             uint8_t o1, o2;
131
132             if (!ptr)
133                 break;
134
135             if (av_sscanf(ptr, "%c%c%c%c", &c1, &c2, &c3, &c4) != 4)
136                 break;
137             o1 = convert(c2) | (convert(c1) << 4);
138             o2 = convert(c4) | (convert(c3) << 4);
139
140             lline = NULL;
141
142             if (i > 12 && o1 == 0x94 && o2 == 0x20 && saveptr &&
143                 (av_strncasecmp(saveptr, "942f", 4) || !av_strncasecmp(saveptr, "942c", 4))) {
144
145                 out[i] = 0;
146
147                 sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
148                 if (!sub)
149                     goto fail;
150
151                 sub->pos = pos;
152                 pos += i;
153                 sub->pts = ts;
154                 sub->duration = i * 11;
155                 ts += sub->duration;
156                 i = 0;
157             }
158
159             out[i+0] = 0xfc;
160             out[i+1] = o1;
161             out[i+2] = o2;
162         }
163
164         out[i] = 0;
165
166         sub = ff_subtitles_queue_insert(&scc->q, out, i, 0);
167         if (!sub)
168             goto fail;
169
170         sub->pos = pos;
171         sub->pts = ts;
172         sub->duration = next_ts - ts;
173         ts = next_ts;
174     }
175
176     ff_subtitles_queue_finalize(s, &scc->q);
177
178     return ret;
179 fail:
180     ff_subtitles_queue_clean(&scc->q);
181     return AVERROR(ENOMEM);
182 }
183
184 static int scc_read_packet(AVFormatContext *s, AVPacket *pkt)
185 {
186     SCCContext *scc = s->priv_data;
187     return ff_subtitles_queue_read_packet(&scc->q, pkt);
188 }
189
190 static int scc_read_seek(AVFormatContext *s, int stream_index,
191                          int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
192 {
193     SCCContext *scc = s->priv_data;
194     return ff_subtitles_queue_seek(&scc->q, s, stream_index,
195                                    min_ts, ts, max_ts, flags);
196 }
197
198 static int scc_read_close(AVFormatContext *s)
199 {
200     SCCContext *scc = s->priv_data;
201     ff_subtitles_queue_clean(&scc->q);
202     return 0;
203 }
204
205 AVInputFormat ff_scc_demuxer = {
206     .name           = "scc",
207     .long_name      = NULL_IF_CONFIG_SMALL("Scenarist Closed Captions"),
208     .priv_data_size = sizeof(SCCContext),
209     .read_probe     = scc_probe,
210     .read_header    = scc_read_header,
211     .read_packet    = scc_read_packet,
212     .read_seek2     = scc_read_seek,
213     .read_close     = scc_read_close,
214     .extensions     = "scc",
215 };