subs: support loading external ASS subtitles via stream layer

Previously the argument of the "-sub" option was always interpreted as
a local filename when trying to read it as a libass file. Use the
stream layer to read it instead, so that protocols like 'http://' are
also supported like they are for the main video/audio file.

Based on patch by Yuriy Kaminskiy <yumkam@mail.ru>
This commit is contained in:
Uoti Urpala 2010-03-09 22:35:53 +02:00
parent 173beb5608
commit 9786d38667
3 changed files with 55 additions and 2 deletions

View File

@ -26,10 +26,13 @@
#include <ass/ass.h>
#include <ass/ass_types.h>
#include <libavutil/common.h>
#include "mp_msg.h"
#include "get_path.h"
#include "ass_mp.h"
#include "subreader.h"
#include "stream/stream.h"
#ifdef CONFIG_FONTCONFIG
#include <fontconfig/fontconfig.h>
@ -227,6 +230,55 @@ ASS_Track *ass_read_subdata(ASS_Library *library, sub_data *subdata,
return track;
}
ASS_Track *ass_read_stream(ASS_Library *library, char *fname, char *charset)
{
int i;
char *buf = NULL;
ASS_Track *track;
size_t sz = 0;
size_t buf_alloc = 0;
stream_t *fd;
fd = open_stream(fname, NULL, &i);
if (!fd)
// Stream code should have printed an error already
return NULL;
if (fd->end_pos > STREAM_BUFFER_SIZE)
/* read entire file if size is known */
buf_alloc = fd->end_pos;
else
buf_alloc = 1000;
for (;;) {
if (sz > 100000000) {
mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
"larger than 100 MB: %s\n", fname);
sz = 0;
break;
}
buf_alloc = FFMAX(buf_alloc, sz + (sz >> 1));
buf_alloc = FFMIN(buf_alloc, 100000001);
buf = realloc(buf, buf_alloc + 1);
i = stream_read(fd, buf + sz, buf_alloc - sz);
if (i <= 0)
break;
sz += i;
}
free_stream(fd);
if (!sz) {
free(buf);
return NULL;
}
buf[sz] = 0;
buf = realloc(buf, sz + 1);
track = ass_read_memory(library, buf, sz, charset);
if (track) {
free(track->name);
track->name = strdup(fname);
}
free(buf);
return track;
}
void ass_configure(ASS_Renderer *priv, int w, int h, int unscaled)
{
int hinting;

View File

@ -48,6 +48,7 @@ ASS_Track *ass_default_track(ASS_Library *library);
int ass_process_subtitle(ASS_Track *track, subtitle *sub);
ASS_Track *ass_read_subdata(ASS_Library *library, sub_data *subdata,
double fps);
ASS_Track *ass_read_stream(ASS_Library *library, char *fname, char *charset);
void ass_configure(ASS_Renderer *priv, int w, int h, int hinting);
void ass_configure_fonts(ASS_Renderer *priv);

View File

@ -1117,9 +1117,9 @@ void add_subtitles(struct MPContext *mpctx, char *filename, float fps, int noerr
#ifdef CONFIG_ASS
if (opts->ass_enabled) {
#ifdef CONFIG_ICONV
asst = ass_read_file(ass_library, filename, sub_cp);
asst = ass_read_stream(ass_library, filename, sub_cp);
#else
asst = ass_read_file(ass_library, filename, 0);
asst = ass_read_stream(ass_library, filename, 0);
#endif
if (!asst) {
subd = sub_read_file(filename, fps);