Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * SSA/ASS demuxer | ||
3 | * Copyright (c) 2008 Michael Niedermayer | ||
4 | * Copyright (c) 2014 Clément Bœsch | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | #include <stdint.h> | ||
24 | |||
25 | #include "avformat.h" | ||
26 | #include "demux.h" | ||
27 | #include "internal.h" | ||
28 | #include "subtitles.h" | ||
29 | #include "libavutil/bprint.h" | ||
30 | |||
31 | typedef struct ASSContext { | ||
32 | FFDemuxSubtitlesQueue q; | ||
33 | unsigned readorder; | ||
34 | } ASSContext; | ||
35 | |||
36 | 7186 | static int ass_probe(const AVProbeData *p) | |
37 | { | ||
38 | char buf[13]; | ||
39 | FFTextReader tr; | ||
40 | 7186 | ff_text_init_buf(&tr, p->buf, p->buf_size); | |
41 | |||
42 |
4/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7189 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 7186 times.
|
7193 | while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n') |
43 | 7 | ff_text_r8(&tr); | |
44 | |||
45 | 7186 | ff_text_read(&tr, buf, sizeof(buf)); | |
46 | |||
47 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7183 times.
|
7186 | if (!memcmp(buf, "[Script Info]", 13)) |
48 | 3 | return AVPROBE_SCORE_MAX; | |
49 | |||
50 | 7183 | return 0; | |
51 | } | ||
52 | |||
53 | 208 | static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p, | |
54 | int64_t *start, int *duration) | ||
55 | { | ||
56 | 208 | int pos = 0; | |
57 | int64_t end; | ||
58 | int hh1, mm1, ss1, ms1; | ||
59 | int hh2, mm2, ss2, ms2; | ||
60 | |||
61 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 157 times.
|
208 | if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n", |
62 | &hh1, &mm1, &ss1, &ms1, | ||
63 |
1/2✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
51 | &hh2, &mm2, &ss2, &ms2, &pos) >= 8 && pos > 0) { |
64 | |||
65 | /* This is not part of the sscanf itself in order to handle an actual | ||
66 | * number (which would be the Layer) or the form "Marked=N" (which is | ||
67 | * the old SSA field, now replaced by Layer, and will lead to Layer | ||
68 | * being 0 here). */ | ||
69 | 51 | const int layer = atoi(p + 10); | |
70 | |||
71 | 51 | end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2; | |
72 | 51 | *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1; | |
73 | 51 | *duration = end - *start; | |
74 | |||
75 | 51 | av_bprint_clear(dst); | |
76 | 51 | av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos); | |
77 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
|
51 | if (!av_bprint_is_complete(dst)) |
78 | ✗ | return AVERROR(ENOMEM); | |
79 | |||
80 | /* right strip the buffer */ | ||
81 | 51 | while (dst->len > 0 && | |
82 |
3/4✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 102 times.
|
153 | dst->str[dst->len - 1] == '\r' || |
83 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 51 times.
|
102 | dst->str[dst->len - 1] == '\n') |
84 | 102 | dst->str[--dst->len] = 0; | |
85 | 51 | return 0; | |
86 | } | ||
87 | 157 | return -1; | |
88 | } | ||
89 | |||
90 | 211 | static int64_t get_line(AVBPrint *buf, FFTextReader *tr) | |
91 | { | ||
92 | 211 | int64_t pos = ff_text_pos(tr); | |
93 | |||
94 | 211 | av_bprint_clear(buf); | |
95 | 16354 | for (;;) { | |
96 | 16565 | char c = ff_text_r8(tr); | |
97 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16562 times.
|
16565 | if (!c) |
98 | 3 | break; | |
99 | 16562 | av_bprint_chars(buf, c, 1); | |
100 |
2/2✓ Branch 0 taken 208 times.
✓ Branch 1 taken 16354 times.
|
16562 | if (c == '\n') |
101 | 208 | break; | |
102 | } | ||
103 | 211 | return pos; | |
104 | } | ||
105 | |||
106 | 3 | static int ass_read_header(AVFormatContext *s) | |
107 | { | ||
108 | 3 | ASSContext *ass = s->priv_data; | |
109 | AVBPrint header, line, rline; | ||
110 | 3 | int res = 0; | |
111 | AVStream *st; | ||
112 | FFTextReader tr; | ||
113 | 3 | ff_text_init_avio(s, &tr, s->pb); | |
114 | |||
115 | 3 | st = avformat_new_stream(s, NULL); | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
117 | ✗ | return AVERROR(ENOMEM); | |
118 | 3 | avpriv_set_pts_info(st, 64, 1, 100); | |
119 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
120 | 3 | st->codecpar->codec_id = AV_CODEC_ID_ASS; | |
121 | |||
122 | 3 | av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED); | |
123 | 3 | av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED); | |
124 | 3 | av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED); | |
125 | |||
126 | 3 | ass->q.keep_duplicates = 1; | |
127 | |||
128 | 208 | for (;;) { | |
129 | 211 | int64_t pos = get_line(&line, &tr); | |
130 | 211 | int64_t ts_start = AV_NOPTS_VALUE; | |
131 | 211 | int duration = -1; | |
132 | AVPacket *sub; | ||
133 | |||
134 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 208 times.
|
211 | if (!line.str[0]) // EOF |
135 | 3 | break; | |
136 | |||
137 |
2/2✓ Branch 1 taken 157 times.
✓ Branch 2 taken 51 times.
|
208 | if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) { |
138 | 157 | av_bprintf(&header, "%s", line.str); | |
139 | 157 | continue; | |
140 | } | ||
141 | 51 | sub = ff_subtitles_queue_insert_bprint(&ass->q, &rline, 0); | |
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
|
51 | if (!sub) { |
143 | ✗ | res = AVERROR(ENOMEM); | |
144 | ✗ | goto end; | |
145 | } | ||
146 | 51 | sub->pos = pos; | |
147 | 51 | sub->pts = ts_start; | |
148 | 51 | sub->duration = duration; | |
149 | } | ||
150 | |||
151 | 3 | res = ff_bprint_to_codecpar_extradata(st->codecpar, &header); | |
152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (res < 0) |
153 | ✗ | goto end; | |
154 | |||
155 | 3 | ff_subtitles_queue_finalize(s, &ass->q); | |
156 | |||
157 | 3 | end: | |
158 | 3 | av_bprint_finalize(&header, NULL); | |
159 | 3 | av_bprint_finalize(&line, NULL); | |
160 | 3 | av_bprint_finalize(&rline, NULL); | |
161 | 3 | return res; | |
162 | } | ||
163 | |||
164 | const FFInputFormat ff_ass_demuxer = { | ||
165 | .p.name = "ass", | ||
166 | .p.long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"), | ||
167 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
168 | .priv_data_size = sizeof(ASSContext), | ||
169 | .read_probe = ass_probe, | ||
170 | .read_header = ass_read_header, | ||
171 | .read_packet = ff_subtitles_read_packet, | ||
172 | .read_close = ff_subtitles_read_close, | ||
173 | .read_seek2 = ff_subtitles_read_seek, | ||
174 | }; | ||
175 |