yading@11
|
1 /*
|
yading@11
|
2 * Copyright (c) 2012 Clément Bœsch
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of FFmpeg.
|
yading@11
|
5 *
|
yading@11
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 /**
|
yading@11
|
22 * @file
|
yading@11
|
23 * SubViewer subtitle demuxer
|
yading@11
|
24 * @see https://en.wikipedia.org/wiki/SubViewer
|
yading@11
|
25 */
|
yading@11
|
26
|
yading@11
|
27 #include "avformat.h"
|
yading@11
|
28 #include "internal.h"
|
yading@11
|
29 #include "subtitles.h"
|
yading@11
|
30 #include "libavcodec/internal.h"
|
yading@11
|
31 #include "libavutil/avstring.h"
|
yading@11
|
32 #include "libavutil/bprint.h"
|
yading@11
|
33 #include "libavutil/intreadwrite.h"
|
yading@11
|
34
|
yading@11
|
35 typedef struct {
|
yading@11
|
36 FFDemuxSubtitlesQueue q;
|
yading@11
|
37 } SubViewerContext;
|
yading@11
|
38
|
yading@11
|
39 static int subviewer_probe(AVProbeData *p)
|
yading@11
|
40 {
|
yading@11
|
41 char c;
|
yading@11
|
42 const unsigned char *ptr = p->buf;
|
yading@11
|
43
|
yading@11
|
44 if (AV_RB24(ptr) == 0xEFBBBF)
|
yading@11
|
45 ptr += 3; /* skip UTF-8 BOM */
|
yading@11
|
46 if (sscanf(ptr, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1)
|
yading@11
|
47 return AVPROBE_SCORE_MAX/2;
|
yading@11
|
48 if (!strncmp(ptr, "[INFORMATION]", 13))
|
yading@11
|
49 return AVPROBE_SCORE_MAX/3;
|
yading@11
|
50 return 0;
|
yading@11
|
51 }
|
yading@11
|
52
|
yading@11
|
53 static int read_ts(const char *s, int64_t *start, int *duration)
|
yading@11
|
54 {
|
yading@11
|
55 int64_t end;
|
yading@11
|
56 int hh1, mm1, ss1, ms1;
|
yading@11
|
57 int hh2, mm2, ss2, ms2;
|
yading@11
|
58
|
yading@11
|
59 if (sscanf(s, "%u:%u:%u.%u,%u:%u:%u.%u",
|
yading@11
|
60 &hh1, &mm1, &ss1, &ms1, &hh2, &mm2, &ss2, &ms2) == 8) {
|
yading@11
|
61 end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
|
yading@11
|
62 *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
|
yading@11
|
63 *duration = end - *start;
|
yading@11
|
64 return 0;
|
yading@11
|
65 }
|
yading@11
|
66 return -1;
|
yading@11
|
67 }
|
yading@11
|
68
|
yading@11
|
69 static int subviewer_read_header(AVFormatContext *s)
|
yading@11
|
70 {
|
yading@11
|
71 SubViewerContext *subviewer = s->priv_data;
|
yading@11
|
72 AVStream *st = avformat_new_stream(s, NULL);
|
yading@11
|
73 AVBPrint header;
|
yading@11
|
74 int res = 0, new_event = 1;
|
yading@11
|
75 int64_t pts_start = AV_NOPTS_VALUE;
|
yading@11
|
76 int duration = -1;
|
yading@11
|
77 AVPacket *sub = NULL;
|
yading@11
|
78
|
yading@11
|
79 if (!st)
|
yading@11
|
80 return AVERROR(ENOMEM);
|
yading@11
|
81 avpriv_set_pts_info(st, 64, 1, 100);
|
yading@11
|
82 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
|
yading@11
|
83 st->codec->codec_id = AV_CODEC_ID_SUBVIEWER;
|
yading@11
|
84
|
yading@11
|
85 av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
|
yading@11
|
86
|
yading@11
|
87 while (!url_feof(s->pb)) {
|
yading@11
|
88 char line[2048];
|
yading@11
|
89 int64_t pos = 0;
|
yading@11
|
90 int len = ff_get_line(s->pb, line, sizeof(line));
|
yading@11
|
91
|
yading@11
|
92 if (!len)
|
yading@11
|
93 break;
|
yading@11
|
94
|
yading@11
|
95 line[strcspn(line, "\r\n")] = 0;
|
yading@11
|
96
|
yading@11
|
97 if (line[0] == '[' && strncmp(line, "[br]", 4)) {
|
yading@11
|
98
|
yading@11
|
99 /* ignore event style, XXX: add to side_data? */
|
yading@11
|
100 if (strstr(line, "[COLF]") || strstr(line, "[SIZE]") ||
|
yading@11
|
101 strstr(line, "[FONT]") || strstr(line, "[STYLE]"))
|
yading@11
|
102 continue;
|
yading@11
|
103
|
yading@11
|
104 if (!st->codec->extradata) { // header not finalized yet
|
yading@11
|
105 av_bprintf(&header, "%s\n", line);
|
yading@11
|
106 if (!strncmp(line, "[END INFORMATION]", 17) || !strncmp(line, "[SUBTITLE]", 10)) {
|
yading@11
|
107 /* end of header */
|
yading@11
|
108 res = avpriv_bprint_to_extradata(st->codec, &header);
|
yading@11
|
109 if (res < 0)
|
yading@11
|
110 goto end;
|
yading@11
|
111 } else if (strncmp(line, "[INFORMATION]", 13)) {
|
yading@11
|
112 /* assume file metadata at this point */
|
yading@11
|
113 int i, j = 0;
|
yading@11
|
114 char key[32], value[128];
|
yading@11
|
115
|
yading@11
|
116 for (i = 1; i < sizeof(key) - 1 && line[i] && line[i] != ']'; i++)
|
yading@11
|
117 key[i - 1] = av_tolower(line[i]);
|
yading@11
|
118 key[i - 1] = 0;
|
yading@11
|
119
|
yading@11
|
120 if (line[i] == ']')
|
yading@11
|
121 i++;
|
yading@11
|
122 while (line[i] == ' ')
|
yading@11
|
123 i++;
|
yading@11
|
124 while (j < sizeof(value) - 1 && line[i] && line[i] != ']')
|
yading@11
|
125 value[j++] = line[i++];
|
yading@11
|
126 value[j] = 0;
|
yading@11
|
127
|
yading@11
|
128 av_dict_set(&s->metadata, key, value, 0);
|
yading@11
|
129 }
|
yading@11
|
130 }
|
yading@11
|
131 } else if (read_ts(line, &pts_start, &duration) >= 0) {
|
yading@11
|
132 new_event = 1;
|
yading@11
|
133 pos = avio_tell(s->pb);
|
yading@11
|
134 } else if (*line) {
|
yading@11
|
135 if (!new_event) {
|
yading@11
|
136 sub = ff_subtitles_queue_insert(&subviewer->q, "\n", 1, 1);
|
yading@11
|
137 if (!sub) {
|
yading@11
|
138 res = AVERROR(ENOMEM);
|
yading@11
|
139 goto end;
|
yading@11
|
140 }
|
yading@11
|
141 }
|
yading@11
|
142 sub = ff_subtitles_queue_insert(&subviewer->q, line, strlen(line), !new_event);
|
yading@11
|
143 if (!sub) {
|
yading@11
|
144 res = AVERROR(ENOMEM);
|
yading@11
|
145 goto end;
|
yading@11
|
146 }
|
yading@11
|
147 if (new_event) {
|
yading@11
|
148 sub->pos = pos;
|
yading@11
|
149 sub->pts = pts_start;
|
yading@11
|
150 sub->duration = duration;
|
yading@11
|
151 }
|
yading@11
|
152 new_event = 0;
|
yading@11
|
153 }
|
yading@11
|
154 }
|
yading@11
|
155
|
yading@11
|
156 ff_subtitles_queue_finalize(&subviewer->q);
|
yading@11
|
157
|
yading@11
|
158 end:
|
yading@11
|
159 av_bprint_finalize(&header, NULL);
|
yading@11
|
160 return res;
|
yading@11
|
161 }
|
yading@11
|
162
|
yading@11
|
163 static int subviewer_read_packet(AVFormatContext *s, AVPacket *pkt)
|
yading@11
|
164 {
|
yading@11
|
165 SubViewerContext *subviewer = s->priv_data;
|
yading@11
|
166 return ff_subtitles_queue_read_packet(&subviewer->q, pkt);
|
yading@11
|
167 }
|
yading@11
|
168
|
yading@11
|
169 static int subviewer_read_seek(AVFormatContext *s, int stream_index,
|
yading@11
|
170 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
|
yading@11
|
171 {
|
yading@11
|
172 SubViewerContext *subviewer = s->priv_data;
|
yading@11
|
173 return ff_subtitles_queue_seek(&subviewer->q, s, stream_index,
|
yading@11
|
174 min_ts, ts, max_ts, flags);
|
yading@11
|
175 }
|
yading@11
|
176
|
yading@11
|
177 static int subviewer_read_close(AVFormatContext *s)
|
yading@11
|
178 {
|
yading@11
|
179 SubViewerContext *subviewer = s->priv_data;
|
yading@11
|
180 ff_subtitles_queue_clean(&subviewer->q);
|
yading@11
|
181 return 0;
|
yading@11
|
182 }
|
yading@11
|
183
|
yading@11
|
184 AVInputFormat ff_subviewer_demuxer = {
|
yading@11
|
185 .name = "subviewer",
|
yading@11
|
186 .long_name = NULL_IF_CONFIG_SMALL("SubViewer subtitle format"),
|
yading@11
|
187 .priv_data_size = sizeof(SubViewerContext),
|
yading@11
|
188 .read_probe = subviewer_probe,
|
yading@11
|
189 .read_header = subviewer_read_header,
|
yading@11
|
190 .read_packet = subviewer_read_packet,
|
yading@11
|
191 .read_seek2 = subviewer_read_seek,
|
yading@11
|
192 .read_close = subviewer_read_close,
|
yading@11
|
193 .extensions = "sub",
|
yading@11
|
194 };
|