diff libass/ass_mp.c @ 30473:7446f58b6899

Add support for loading ASS subtitles through the stream layer and thus e.g. from some network location. Patch by Yuriy Kaminskiy [yumkam mail ru]
author reimar
date Fri, 05 Feb 2010 17:13:47 +0000
parents 871e061553bb
children 1f2ecb6a3f3d
line wrap: on
line diff
--- a/libass/ass_mp.c	Fri Feb 05 16:42:13 2010 +0000
+++ b/libass/ass_mp.c	Fri Feb 05 17:13:47 2010 +0000
@@ -28,6 +28,8 @@
 #include "get_path.h"
 
 #include "ass_mp.h"
+#include "help_mp.h"
+#include "stream/stream.h"
 
 #ifdef CONFIG_FONTCONFIG
 #include <fontconfig/fontconfig.h>
@@ -216,6 +218,51 @@
 	return track;
 }
 
+ass_track_t* ass_read_stream(ass_library_t* library, char *fname, char *charset) {
+	int i;
+	char *buf = NULL;
+	ass_track_t *track;
+	size_t sz = 0;
+	size_t buf_alloc = 0;
+	stream_t *fd;
+
+	fd = open_stream(fname, NULL, &i);
+	if (!fd) {
+		mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FopenFailed, fname);
+		return NULL;
+	}
+	if (fd->end_pos > STREAM_BUFFER_SIZE)
+		/* read entire file if size is known */
+		buf_alloc = fd->end_pos;
+	for (;;) {
+		if (buf_alloc >= 100*1024*1024) {
+			mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_RefusingToLoadSubtitlesLargerThan100M, fname);
+			sz = 0;
+			break;
+		}
+		if (buf_alloc < sz + STREAM_BUFFER_SIZE)
+			buf_alloc += STREAM_BUFFER_SIZE;
+		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_t* priv, int w, int h, int unscaled) {
 	int hinting;
 	ass_set_frame_size(priv, w, h);