Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * MCC subtitle demuxer | ||
3 | * Copyright (c) 2020 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 MCCContext { | ||
30 | FFDemuxSubtitlesQueue q; | ||
31 | } MCCContext; | ||
32 | |||
33 | 6924 | static int mcc_probe(const AVProbeData *p) | |
34 | { | ||
35 | char buf[28]; | ||
36 | FFTextReader tr; | ||
37 | |||
38 | 6924 | ff_text_init_buf(&tr, p->buf, p->buf_size); | |
39 | |||
40 |
4/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6927 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 6924 times.
|
6931 | while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n') |
41 | 7 | ff_text_r8(&tr); | |
42 | |||
43 | 6924 | ff_text_read(&tr, buf, sizeof(buf)); | |
44 | |||
45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6924 times.
|
6924 | if (!memcmp(buf, "File Format=MacCaption_MCC V", 28)) |
46 | ✗ | return AVPROBE_SCORE_MAX; | |
47 | |||
48 | 6924 | 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 | typedef struct alias { | ||
63 | uint8_t key; | ||
64 | int len; | ||
65 | const char *value; | ||
66 | } alias; | ||
67 | |||
68 | static const alias aliases[20] = { | ||
69 | { .key = 16, .len = 3, .value = "\xFA\x0\x0", }, | ||
70 | { .key = 17, .len = 6, .value = "\xFA\x0\x0\xFA\x0\x0", }, | ||
71 | { .key = 18, .len = 9, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
72 | { .key = 19, .len = 12, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
73 | { .key = 20, .len = 15, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
74 | { .key = 21, .len = 18, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
75 | { .key = 22, .len = 21, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
76 | { .key = 23, .len = 24, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
77 | { .key = 24, .len = 27, .value = "\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0\xFA\x0\x0", }, | ||
78 | { .key = 25, .len = 3, .value = "\xFB\x80\x80", }, | ||
79 | { .key = 26, .len = 3, .value = "\xFC\x80\x80", }, | ||
80 | { .key = 27, .len = 3, .value = "\xFD\x80\x80", }, | ||
81 | { .key = 28, .len = 2, .value = "\x96\x69", }, | ||
82 | { .key = 29, .len = 2, .value = "\x61\x01", }, | ||
83 | { .key = 30, .len = 3, .value = "\xFC\x80\x80", }, | ||
84 | { .key = 31, .len = 3, .value = "\xFC\x80\x80", }, | ||
85 | { .key = 32, .len = 4, .value = "\xE1\x00\x00\x00", }, | ||
86 | { .key = 33, .len = 0, .value = NULL, }, | ||
87 | { .key = 34, .len = 0, .value = NULL, }, | ||
88 | { .key = 35, .len = 1, .value = "\x0", }, | ||
89 | }; | ||
90 | |||
91 | ✗ | static int mcc_read_header(AVFormatContext *s) | |
92 | { | ||
93 | ✗ | MCCContext *mcc = s->priv_data; | |
94 | ✗ | AVStream *st = avformat_new_stream(s, NULL); | |
95 | AVRational rate; | ||
96 | int64_t ts, pos; | ||
97 | uint8_t out[4096]; | ||
98 | char line[4096]; | ||
99 | FFTextReader tr; | ||
100 | ✗ | int ret = 0; | |
101 | |||
102 | ✗ | ff_text_init_avio(s, &tr, s->pb); | |
103 | |||
104 | ✗ | if (!st) | |
105 | ✗ | return AVERROR(ENOMEM); | |
106 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
107 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_EIA_608; | |
108 | ✗ | avpriv_set_pts_info(st, 64, 1, 30); | |
109 | |||
110 | ✗ | while (!ff_text_eof(&tr)) { | |
111 | ✗ | int hh, mm, ss, fs, i = 0, j = 0; | |
112 | ✗ | int start = 12, count = 0; | |
113 | AVPacket *sub; | ||
114 | char *lline; | ||
115 | |||
116 | ✗ | ff_subtitles_read_line(&tr, line, sizeof(line)); | |
117 | ✗ | if (!strncmp(line, "File Format=MacCaption_MCC V", 28)) | |
118 | ✗ | continue; | |
119 | ✗ | if (!strncmp(line, "//", 2)) | |
120 | ✗ | continue; | |
121 | ✗ | if (!strncmp(line, "Time Code Rate=", 15)) { | |
122 | ✗ | char *rate_str = line + 15; | |
123 | ✗ | char *df = NULL; | |
124 | ✗ | int num = -1, den = -1; | |
125 | |||
126 | ✗ | if (rate_str[0]) { | |
127 | ✗ | num = strtol(rate_str, &df, 10); | |
128 | ✗ | den = 1; | |
129 | ✗ | if (df && !av_strncasecmp(df, "DF", 2)) { | |
130 | ✗ | av_reduce(&num, &den, num * 1000LL, 1001, INT_MAX); | |
131 | } | ||
132 | } | ||
133 | |||
134 | ✗ | if (num > 0 && den > 0) { | |
135 | ✗ | rate = av_make_q(num, den); | |
136 | ✗ | avpriv_set_pts_info(st, 64, rate.den, rate.num); | |
137 | } | ||
138 | ✗ | continue; | |
139 | } | ||
140 | |||
141 | ✗ | if (av_sscanf(line, "%d:%d:%d:%d", &hh, &mm, &ss, &fs) != 4) | |
142 | ✗ | continue; | |
143 | |||
144 | ✗ | ts = av_sat_add64(av_rescale(hh * 3600LL + mm * 60LL + ss, rate.num, rate.den), fs); | |
145 | |||
146 | ✗ | lline = (char *)&line; | |
147 | ✗ | lline += 12; | |
148 | ✗ | pos = ff_text_pos(&tr); | |
149 | |||
150 | ✗ | while (lline[i]) { | |
151 | ✗ | uint8_t v = convert(lline[i]); | |
152 | |||
153 | ✗ | if (v >= 16 && v <= 35) { | |
154 | ✗ | int idx = v - 16; | |
155 | ✗ | if (aliases[idx].len) { | |
156 | ✗ | if (j >= sizeof(out) - 1 - aliases[idx].len) { | |
157 | ✗ | j = 0; | |
158 | ✗ | break; | |
159 | } | ||
160 | ✗ | memcpy(out + j, aliases[idx].value, aliases[idx].len); | |
161 | ✗ | j += aliases[idx].len; | |
162 | } | ||
163 | } else { | ||
164 | uint8_t vv; | ||
165 | |||
166 | ✗ | if (i + 13 >= sizeof(line) - 1) | |
167 | ✗ | break; | |
168 | ✗ | vv = convert(lline[i + 1]); | |
169 | ✗ | if (j >= sizeof(out) - 1) { | |
170 | ✗ | j = 0; | |
171 | ✗ | break; | |
172 | } | ||
173 | ✗ | out[j++] = vv | (v << 4); | |
174 | ✗ | i++; | |
175 | } | ||
176 | |||
177 | ✗ | i++; | |
178 | } | ||
179 | ✗ | out[j] = 0; | |
180 | |||
181 | ✗ | if (out[7] & 0x80) | |
182 | ✗ | start += 4; | |
183 | ✗ | count = (out[11] & 0x1f) * 3; | |
184 | ✗ | if (j < start + count + 1) | |
185 | ✗ | continue; | |
186 | |||
187 | ✗ | if (!count) | |
188 | ✗ | continue; | |
189 | ✗ | sub = ff_subtitles_queue_insert(&mcc->q, out + start, count, 0); | |
190 | ✗ | if (!sub) | |
191 | ✗ | return AVERROR(ENOMEM); | |
192 | |||
193 | ✗ | sub->pos = pos; | |
194 | ✗ | sub->pts = ts; | |
195 | ✗ | sub->duration = 1; | |
196 | } | ||
197 | |||
198 | ✗ | ff_subtitles_queue_finalize(s, &mcc->q); | |
199 | |||
200 | ✗ | return ret; | |
201 | } | ||
202 | |||
203 | const AVInputFormat ff_mcc_demuxer = { | ||
204 | .name = "mcc", | ||
205 | .long_name = NULL_IF_CONFIG_SMALL("MacCaption"), | ||
206 | .priv_data_size = sizeof(MCCContext), | ||
207 | .flags_internal = FF_FMT_INIT_CLEANUP, | ||
208 | .read_probe = mcc_probe, | ||
209 | .read_header = mcc_read_header, | ||
210 | .extensions = "mcc", | ||
211 | .read_packet = ff_subtitles_read_packet, | ||
212 | .read_seek2 = ff_subtitles_read_seek, | ||
213 | .read_close = ff_subtitles_read_close, | ||
214 | }; | ||
215 |