History

avformat: add MCC demuxer

Fixes #7680

Paul B Mahol committed 6 years ago

Commit 71f19bf1 parent b52ca2c5 files · +243 −2Browse files

Unified · Split

5 files
  1. Changelog +1 −0
  2. libavformat/Makefile +1 −0
  3. libavformat/allformats.c +1 −0
  4. libavformat/mccdec.c +238 −0
  5. libavformat/version.h +2 −2
Changelog+1−0
@@ -3,6 +3,7 @@ releases are sorted from youngest to oldest. · 2 unmodified lines
33
44 version <next>:
55 - AudioToolbox output device
6+- MacCaption demuxer
67
78
89 version 4.3:
libavformat/Makefile+1−0
@@ -303,6 +303,7 @@ OBJS-$(CONFIG_MATROSKA_MUXER) += matroskaenc.o matroska.o \ · 302 unmodified lines
303303 av1.o avc.o hevc.o \
304304 flacenc_header.o avlanguage.o \
305305 vorbiscomment.o wv.o
306+OBJS-$(CONFIG_MCC_DEMUXER) += mccdec.o subtitles.o
306307 OBJS-$(CONFIG_MD5_MUXER) += hashenc.o
307308 OBJS-$(CONFIG_MGSTS_DEMUXER) += mgsts.o
308309 OBJS-$(CONFIG_MICRODVD_DEMUXER) += microdvddec.o subtitles.o
libavformat/allformats.c+1−0
@@ -230,6 +230,7 @@ extern AVInputFormat ff_lvf_demuxer; · 229 unmodified lines
230230 extern AVInputFormat ff_lxf_demuxer;
231231 extern AVInputFormat ff_m4v_demuxer;
232232 extern AVOutputFormat ff_m4v_muxer;
233+extern AVInputFormat ff_mcc_demuxer;
233234 extern AVOutputFormat ff_md5_muxer;
234235 extern AVInputFormat ff_matroska_demuxer;
235236 extern AVOutputFormat ff_matroska_muxer;
libavformat/mccdec.c+238−0
@@ -0,0 +1,238 @@
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+static int mcc_probe(const AVProbeData *p)
34+{
35+ char buf[28];
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, "File Format=MacCaption_MCC V", 28))
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+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+ num *= 1000;
131+ den = 1001;
132+ }
133+ }
134+
135+ if (num > 0 && den > 0) {
136+ rate = av_make_q(num, den);
137+ avpriv_set_pts_info(st, 64, rate.den, rate.num);
138+ }
139+ continue;
140+ }
141+
142+ if (av_sscanf(line, "%d:%d:%d:%d", &hh, &mm, &ss, &fs) != 4)
143+ continue;
144+
145+ ts = av_rescale(hh * 3600LL + mm * 60LL + ss, rate.num, rate.den) + fs;
146+
147+ lline = (char *)&line;
148+ lline += 12;
149+ pos = ff_text_pos(&tr);
150+
151+ while (lline[i]) {
152+ uint8_t v = convert(lline[i]);
153+
154+ if (v >= 16 && v <= 35) {
155+ int idx = v - 16;
156+ if (aliases[idx].len) {
157+ if (j >= sizeof(out) - 1 - aliases[idx].len) {
158+ j = 0;
159+ break;
160+ }
161+ memcpy(out + j, aliases[idx].value, aliases[idx].len);
162+ j += aliases[idx].len;
163+ }
164+ } else {
165+ uint8_t vv;
166+
167+ if (i + 13 >= sizeof(line) - 1)
168+ break;
169+ vv = convert(lline[i + 1]);
170+ if (j >= sizeof(out) - 1) {
171+ j = 0;
172+ break;
173+ }
174+ out[j++] = vv | (v << 4);
175+ i++;
176+ }
177+
178+ i++;
179+ }
180+ out[j] = 0;
181+
182+ if (out[7] & 0x80)
183+ start += 4;
184+ count = (out[11] & 0x1f) * 3;
185+ if (j < start + count + 1)
186+ continue;
187+
188+ if (!count)
189+ continue;
190+ sub = ff_subtitles_queue_insert(&mcc->q, out + start, count, 0);
191+ if (!sub)
192+ goto fail;
193+
194+ sub->pos = pos;
195+ sub->pts = ts;
196+ sub->duration = 1;
197+ }
198+
199+ ff_subtitles_queue_finalize(s, &mcc->q);
200+
201+ return ret;
202+fail:
203+ ff_subtitles_queue_clean(&mcc->q);
204+ return AVERROR(ENOMEM);
205+}
206+
207+static int mcc_read_packet(AVFormatContext *s, AVPacket *pkt)
208+{
209+ MCCContext *mcc = s->priv_data;
210+ return ff_subtitles_queue_read_packet(&mcc->q, pkt);
211+}
212+
213+static int mcc_read_seek(AVFormatContext *s, int stream_index,
214+ int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
215+{
216+ MCCContext *mcc = s->priv_data;
217+ return ff_subtitles_queue_seek(&mcc->q, s, stream_index,
218+ min_ts, ts, max_ts, flags);
219+}
220+
221+static int mcc_read_close(AVFormatContext *s)
222+{
223+ MCCContext *mcc = s->priv_data;
224+ ff_subtitles_queue_clean(&mcc->q);
225+ return 0;
226+}
227+
228+AVInputFormat ff_mcc_demuxer = {
229+ .name = "mcc",
230+ .long_name = NULL_IF_CONFIG_SMALL("MacCaption"),
231+ .priv_data_size = sizeof(MCCContext),
232+ .read_probe = mcc_probe,
233+ .read_header = mcc_read_header,
234+ .read_packet = mcc_read_packet,
235+ .read_seek2 = mcc_read_seek,
236+ .read_close = mcc_read_close,
237+ .extensions = "mcc",
238+};
libavformat/version.h+2−2
@@ -32,8 +32,8 @@ · 31 unmodified lines
3232 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
3333 // Also please add any ticket numbers that you believe might be affected here
3434 #define LIBAVFORMAT_VERSION_MAJOR 58
35−#define LIBAVFORMAT_VERSION_MINOR 46
36−#define LIBAVFORMAT_VERSION_MICRO 101
35+#define LIBAVFORMAT_VERSION_MINOR 47
36+#define LIBAVFORMAT_VERSION_MICRO 100
3737
3838 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
3939 LIBAVFORMAT_VERSION_MINOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t