diff options
author | wm4 <wm4@nowhere> | 2013-06-11 12:16:42 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2013-06-23 22:33:59 +0200 |
commit | 4f5e12136de717896bf322e75d42de1af09e1c3e (patch) | |
tree | f6b40f0ae7ab2ef410f1449e9626ab1140862836 | |
parent | d064c69e1c958514c3ef4d7871bcd92c8ff92de9 (diff) | |
download | mpv-4f5e12136de717896bf322e75d42de1af09e1c3e.tar.bz2 mpv-4f5e12136de717896bf322e75d42de1af09e1c3e.tar.xz |
stream: remove padding parameter from stream_read_complete()
Seems like a completely unnecessary complication. Instead, always add a
1 byte padding (could be extended if a caller needs it), and clear it.
Also add some documentation. There was some, but it was outdated and
incomplete.
-rw-r--r-- | core/encode_lavc.c | 2 | ||||
-rw-r--r-- | core/input/input.c | 2 | ||||
-rw-r--r-- | demux/demux_cue.c | 2 | ||||
-rw-r--r-- | demux/demux_edl.c | 2 | ||||
-rw-r--r-- | demux/demux_mf.c | 2 | ||||
-rw-r--r-- | stream/stream.c | 26 | ||||
-rw-r--r-- | stream/stream.h | 9 | ||||
-rw-r--r-- | sub/ass_mp.c | 2 | ||||
-rw-r--r-- | video/out/gl_lcms.c | 2 |
9 files changed, 24 insertions, 25 deletions
diff --git a/core/encode_lavc.c b/core/encode_lavc.c index e7c52be221..747e3e67df 100644 --- a/core/encode_lavc.c +++ b/core/encode_lavc.c @@ -404,7 +404,7 @@ static void encode_2pass_prepare(struct encode_lavc_context *ctx, set_to_avdictionary(dictp, "flags", "-pass2"); } else { struct bstr content = stream_read_complete(*bytebuf, NULL, - 1000000000, 1); + 1000000000); if (content.start == NULL) { mp_msg(MSGT_ENCODE, MSGL_WARN, "%s: could not read '%s', " "disabling 2-pass encoding at pass 1\n", diff --git a/core/input/input.c b/core/input/input.c index 2d7569c8e9..dfa7d1e5b4 100644 --- a/core/input/input.c +++ b/core/input/input.c @@ -1737,7 +1737,7 @@ static int parse_config_file(struct input_ctx *ictx, char *file, bool warn) mp_msg(MSGT_INPUT, MSGL_ERR, "Can't open input config file %s.\n", file); return 0; } - bstr res = stream_read_complete(s, NULL, 1000000, 0); + bstr res = stream_read_complete(s, NULL, 1000000); free_stream(s); mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file); int n_binds = parse_config(ictx, false, res, file); diff --git a/demux/demux_cue.c b/demux/demux_cue.c index 04cc3229f3..073fa9d336 100644 --- a/demux/demux_cue.c +++ b/demux/demux_cue.c @@ -39,7 +39,7 @@ static int try_open_file(struct demuxer *demuxer) if (!mp_probe_cue((struct bstr) { buf, len })) return 0; stream_seek(s, 0); - demuxer->file_contents = stream_read_complete(s, demuxer, 1000000, 0); + demuxer->file_contents = stream_read_complete(s, demuxer, 1000000); if (demuxer->file_contents.start == NULL) return 0; if (!mp_probe_cue((struct bstr) { buf, len })) diff --git a/demux/demux_edl.c b/demux/demux_edl.c index 71d75e7f38..c35137ffb2 100644 --- a/demux/demux_edl.c +++ b/demux/demux_edl.c @@ -34,7 +34,7 @@ static int try_open_file(struct demuxer *demuxer) if (strncmp(buf, header, len)) return 0; stream_seek(s, 0); - demuxer->file_contents = stream_read_complete(s, demuxer, 1000000, 0); + demuxer->file_contents = stream_read_complete(s, demuxer, 1000000); if (demuxer->file_contents.start == NULL) return 0; return DEMUXER_TYPE_EDL; diff --git a/demux/demux_mf.c b/demux/demux_mf.c index 0db3fb8add..127ee8474e 100644 --- a/demux/demux_mf.c +++ b/demux/demux_mf.c @@ -78,7 +78,7 @@ static int demux_mf_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds){ if (stream) { stream_seek(stream, 0); - bstr data = stream_read_complete(stream, NULL, MF_MAX_FILE_SIZE, 0); + bstr data = stream_read_complete(stream, NULL, MF_MAX_FILE_SIZE); if (data.len) { demux_packet_t *dp = new_demux_packet(data.len); memcpy(dp->buffer, data.start, data.len); diff --git a/stream/stream.c b/stream/stream.c index 3281e50fe6..99ae6ed103 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -898,20 +898,27 @@ unsigned char *stream_read_line(stream_t *s, unsigned char *mem, int max, return mem; } +// Read the rest of the stream into memory (current pos to EOF), and return it. +// talloc_ctx: used as talloc parent for the returned allocation +// max_size: must be set to >0. If the file is larger than that, it is treated +// as error. This is a minor robustness measure. +// returns: stream contents, or .start/.len set to NULL on error +// If the file was empty, but no error happened, .start will be non-NULL and +// .len will be 0. +// For convenience, the returned buffer is padded with a 0 byte. The padding +// is not included in the returned length. struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, - int max_size, int padding_bytes) + int max_size) { if (max_size > 1000000000) abort(); int bufsize; int total_read = 0; - int padding = FFMAX(padding_bytes, 1); + int padding = 1; char *buf = NULL; if (s->end_pos > max_size) - return (struct bstr){ - NULL, 0 - }; + return (struct bstr){NULL, 0}; if (s->end_pos > 0) bufsize = s->end_pos + padding; else @@ -924,16 +931,13 @@ struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, break; if (bufsize > max_size) { talloc_free(buf); - return (struct bstr){ - NULL, 0 - }; + return (struct bstr){NULL, 0}; } bufsize = FFMIN(bufsize + (bufsize >> 1), max_size + padding); } buf = talloc_realloc_size(talloc_ctx, buf, total_read + padding); - return (struct bstr){ - buf, total_read - }; + memset(&buf[total_read], 0, padding); + return (struct bstr){buf, total_read}; } bool stream_manages_timeline(struct stream *s) diff --git a/stream/stream.h b/stream/stream.h index db58a2fba0..aa1f943c4a 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -299,14 +299,9 @@ int stream_read(stream_t *s, char *mem, int total); int stream_read_partial(stream_t *s, char *buf, int buf_size); struct MPOpts; -/* - * Return allocated buffer for all data until EOF. - * If amount of data would be more than max_size return NULL as data ptr. - * Make the allocated buffer padding_bytes larger than the data read. - * Write number of bytes read at *amount_read. - */ + struct bstr stream_read_complete(struct stream *s, void *talloc_ctx, - int max_size, int padding_bytes); + int max_size); int stream_control(stream_t *s, int cmd, void *arg); void stream_update_size(stream_t *s); void free_stream(stream_t *s); diff --git a/sub/ass_mp.c b/sub/ass_mp.c index 258dd57688..0efbdd9c90 100644 --- a/sub/ass_mp.c +++ b/sub/ass_mp.c @@ -122,7 +122,7 @@ ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname, if (!s) // Stream code should have printed an error already return NULL; - struct bstr content = stream_read_complete(s, NULL, 100000000, 1); + struct bstr content = stream_read_complete(s, NULL, 100000000); if (content.start == NULL) mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file " "larger than 100 MB: %s\n", fname); diff --git a/video/out/gl_lcms.c b/video/out/gl_lcms.c index 6cded6cd09..69f7c7fa12 100644 --- a/video/out/gl_lcms.c +++ b/video/out/gl_lcms.c @@ -87,7 +87,7 @@ static struct bstr load_file(void *talloc_ctx, const char *filename) struct bstr res = {0}; stream_t *s = open_stream(filename, NULL, NULL); if (s) { - res = stream_read_complete(s, talloc_ctx, 1000000000, 0); + res = stream_read_complete(s, talloc_ctx, 1000000000); free_stream(s); } return res; |