2013-06-22 00:09:52 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Note: just wraps libass, and makes the subtitle track available though
|
|
|
|
// sh_sub->track. It doesn't produce packets and doesn't support seeking.
|
|
|
|
|
|
|
|
#include <ass/ass.h>
|
|
|
|
#include <ass/ass_types.h>
|
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
|
|
|
#include "misc/charset_conv.h"
|
2013-06-22 00:09:52 +00:00
|
|
|
#include "stream/stream.h"
|
|
|
|
#include "demux.h"
|
|
|
|
|
2013-06-23 20:17:01 +00:00
|
|
|
#define PROBE_SIZE (8 * 1024)
|
2013-06-22 00:09:52 +00:00
|
|
|
|
2014-03-15 21:17:51 +00:00
|
|
|
static void message_callback(int level, const char *format, va_list va, void *ctx)
|
|
|
|
{
|
|
|
|
// ignore
|
|
|
|
}
|
2013-06-22 00:09:52 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
static int d_check_file(struct demuxer *demuxer, enum demux_check check)
|
2013-06-22 00:09:52 +00:00
|
|
|
{
|
2013-06-23 20:17:01 +00:00
|
|
|
const char *user_cp = demuxer->opts->sub_cp;
|
2013-06-22 00:09:52 +00:00
|
|
|
struct stream *s = demuxer->stream;
|
2013-12-21 19:37:16 +00:00
|
|
|
struct mp_log *log = demuxer->log;
|
2014-03-15 21:17:51 +00:00
|
|
|
|
|
|
|
if (!demuxer->params || !demuxer->params->expect_subtitle)
|
2013-07-11 18:08:12 +00:00
|
|
|
return -1;
|
2013-06-22 00:09:52 +00:00
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
if (check >= DEMUX_CHECK_UNSAFE) {
|
2014-03-15 21:17:51 +00:00
|
|
|
ASS_Library *lib = ass_library_init();
|
|
|
|
if (!lib)
|
|
|
|
return -1;
|
|
|
|
ass_set_message_cb(lib, message_callback, NULL);
|
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
// Probe by loading a part of the beginning of the file with libass.
|
|
|
|
// Incomplete scripts are usually ok, and we hope libass is not verbose
|
|
|
|
// when dealing with (from its perspective) completely broken binary
|
|
|
|
// garbage.
|
|
|
|
|
|
|
|
bstr buf = stream_peek(s, PROBE_SIZE);
|
|
|
|
// Older versions of libass will overwrite the input buffer, and despite
|
|
|
|
// passing length, expect a 0 termination.
|
|
|
|
void *tmp = talloc_size(NULL, buf.len + 1);
|
|
|
|
memcpy(tmp, buf.start, buf.len);
|
|
|
|
buf.start = tmp;
|
|
|
|
buf.start[buf.len] = '\0';
|
2013-12-21 19:37:16 +00:00
|
|
|
bstr cbuf = mp_charset_guess_and_conv_to_utf8(log, buf, user_cp,
|
2013-07-12 19:58:11 +00:00
|
|
|
MP_ICONV_ALLOW_CUTOFF);
|
|
|
|
if (cbuf.start == NULL)
|
|
|
|
cbuf = buf;
|
|
|
|
ASS_Track *track = ass_read_memory(lib, cbuf.start, cbuf.len, NULL);
|
2014-03-15 21:17:51 +00:00
|
|
|
bool ok = !!track;
|
2013-07-12 19:58:11 +00:00
|
|
|
if (cbuf.start != buf.start)
|
|
|
|
talloc_free(cbuf.start);
|
|
|
|
talloc_free(buf.start);
|
2014-03-15 21:17:51 +00:00
|
|
|
if (track)
|
|
|
|
ass_free_track(track);
|
|
|
|
ass_library_done(lib);
|
|
|
|
if (!ok)
|
2013-07-12 19:58:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-22 00:09:52 +00:00
|
|
|
|
|
|
|
// Actually load the full thing.
|
|
|
|
|
2013-07-12 19:58:11 +00:00
|
|
|
bstr buf = stream_read_complete(s, NULL, 100000000);
|
2013-06-22 00:09:52 +00:00
|
|
|
if (!buf.start) {
|
2013-12-21 19:24:20 +00:00
|
|
|
MP_ERR(demuxer, "Refusing to load subtitle file "
|
2013-06-22 00:09:52 +00:00
|
|
|
"larger than 100 MB: %s\n", demuxer->filename);
|
2013-07-11 18:08:12 +00:00
|
|
|
return -1;
|
2013-06-22 00:09:52 +00:00
|
|
|
}
|
2013-12-21 19:37:16 +00:00
|
|
|
bstr cbuf = mp_charset_guess_and_conv_to_utf8(log, buf, user_cp,
|
2013-07-12 19:58:11 +00:00
|
|
|
MP_ICONV_VERBOSE);
|
2013-06-23 20:17:01 +00:00
|
|
|
if (cbuf.start == NULL)
|
|
|
|
cbuf = buf;
|
|
|
|
if (cbuf.start != buf.start)
|
2014-03-15 21:17:51 +00:00
|
|
|
talloc_free(buf.start);
|
|
|
|
talloc_steal(demuxer, cbuf.start);
|
2013-06-22 00:09:52 +00:00
|
|
|
|
|
|
|
struct sh_stream *sh = new_sh_stream(demuxer, STREAM_SUB);
|
|
|
|
sh->codec = "ass";
|
2014-03-15 21:17:51 +00:00
|
|
|
sh->sub->extradata = cbuf.start;
|
|
|
|
sh->sub->extradata_len = cbuf.len;
|
2013-06-22 00:09:52 +00:00
|
|
|
|
2013-11-03 18:21:47 +00:00
|
|
|
demuxer->seekable = true;
|
|
|
|
|
2013-07-11 18:08:12 +00:00
|
|
|
return 0;
|
2013-06-22 00:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct demuxer_desc demuxer_desc_libass = {
|
|
|
|
.name = "libass",
|
2013-07-12 20:12:02 +00:00
|
|
|
.desc = "ASS/SSA subtitles (libass)",
|
2013-07-12 19:58:11 +00:00
|
|
|
.open = d_check_file,
|
2013-06-22 00:09:52 +00:00
|
|
|
};
|