summaryrefslogtreecommitdiffstats
path: root/demux/packet.h
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2019-06-13 19:10:32 +0200
committerwm4 <wm4@nowhere>2019-09-19 20:37:05 +0200
commit17da9071a4a524139e2ef945ed6cde17dd08c99c (patch)
tree66201f387ae33c5f6dbdb283d62066177b0cedf3 /demux/packet.h
parentef507ad50a0933581f39a0bb86dd85fce9d8f7bc (diff)
downloadmpv-17da9071a4a524139e2ef945ed6cde17dd08c99c.tar.bz2
mpv-17da9071a4a524139e2ef945ed6cde17dd08c99c.tar.xz
demux: add a on-disk cache
Somewhat similar to the old --cache-file, except for the demuxer cache. Instead of keeping packet data in memory, it's written to disk and read back when needed. The idea is to reduce main memory usage, while allowing fast seeking in large cached network streams (especially live streams). Keeping the packet metadata on disk would be rather hard (would use mmap or so, or rewrite the entire demux.c packet queue handling), and since it's relatively small, just keep it in memory. Also for simplicity, the disk cache is append-only. If you're watching really long livestreams, and need pruning, you're probably out of luck. This still could be improved by trying to free unused blocks with fallocate(), but since we're writing multiple streams in an interleaved manner, this is slightly hard. Some rather gross ugliness in packet.h: we want to store the file position of the cached data somewhere, but on 32 bit architectures, we don't have any usable 64 bit members for this, just the buf/len fields, which add up to 64 bit - so the shitty union aliases this memory. Error paths untested. Side data (the complicated part of trying to serialize ffmpeg packets) untested. Stream recording had to be adjusted. Some minor details change due to this, but probably nothing important. The change in attempt_range_joining() is because packets in cache have no valid len field. It was a useful check (heuristically finding broken cases), but not a necessary one. Various other approaches were tried. It would be interesting to list them and to mention the pros and cons, but I don't feel like it.
Diffstat (limited to 'demux/packet.h')
-rw-r--r--demux/packet.h23
1 files changed, 19 insertions, 4 deletions
diff --git a/demux/packet.h b/demux/packet.h
index f4570004e8..cd1183d417 100644
--- a/demux/packet.h
+++ b/demux/packet.h
@@ -29,16 +29,29 @@ typedef struct demux_packet {
double duration;
int64_t pos; // position in source file byte stream
- unsigned char *buffer;
- size_t len;
+ union {
+ // Normally valid for packets.
+ struct {
+ unsigned char *buffer;
+ size_t len;
+ };
+
+ // Used if is_cached==true, special uses only.
+ struct {
+ uint64_t pos;
+ } cached_data;
+ };
int stream; // source stream index (typically sh_stream.index)
bool keyframe;
// backward playback
- bool back_restart; // restart point (reverse and return previous frames)
- bool back_preroll; // initial discarded frame for smooth decoder reinit
+ bool back_restart : 1; // restart point (reverse and return previous frames)
+ bool back_preroll : 1; // initial discarded frame for smooth decoder reinit
+
+ // If true, cached_data is valid, while buffer/len are not.
+ bool is_cached : 1;
// segmentation (ordered chapters, EDL)
bool segmented;
@@ -68,4 +81,6 @@ int demux_packet_set_padding(struct demux_packet *dp, int start, int end);
int demux_packet_add_blockadditional(struct demux_packet *dp, uint64_t id,
void *data, size_t size);
+void demux_packet_unref_contents(struct demux_packet *dp);
+
#endif /* MPLAYER_DEMUX_PACKET_H */