Skip to content

Commit

Permalink
Add support for optionally specifying the exact cache filename
Browse files Browse the repository at this point in the history
  • Loading branch information
myrsloik committed Oct 17, 2024
1 parent 72c6787 commit eea1517
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 30 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ It can be used as either a C++ library directly or through the combined VapourSy

## Dependencies

- FFmpeg 6.1.x. Later releases may or may not work but FFmpeg API breakages are quite common and don't always generate compilation errors. Only `libavcodec`, `libavformat`, `libavutil` libraries are required.
- FFmpeg 7.0.x. Later releases may or may not work but FFmpeg API breakages are quite common and don't always generate compilation errors. Only `libavcodec`, `libavformat`, `libavutil` libraries are required.
- xxHash
- libp2p (already included as submodule)

Expand Down Expand Up @@ -92,7 +92,13 @@ Note that the *BSSource* function by default will silently ignore errors when op

*drc_scale*: Apply dynamic range compression to ac3 audio. 0 = None and 1.0 = Normal.

*cachemode*: 0 = Never read or write index to disk, 1 = Always try to read index but only write index to disk when it will make a noticeable difference on subsequent runs, 2 = Always try to read and write index to disk
*cachemode*:

0 = Never read or write index to disk
1 = Always try to read index but only write index to disk when it will make a noticeable difference on subsequent runs and store index files in a subtree of *cachepath*
2 = Always try to read and write index to disk and store index files in a subtree of *cachepath*
3 = Always try to read index but only write index to disk when it will make a noticeable difference on subsequent runs and store index files in the absolute path in *cachepath* with track number and index extension appended
4 = Always try to read and write index to disk and store index files in the absolute path in *cachepath* with track number and index extension appended

*cachepath*: The path where cache files are written. Note that the actual index files are written into subdirectories using based on the source location. Defaults to %LOCALAPPDATA% on Windows and ~/bsindex elsewhere.

Expand Down
18 changes: 9 additions & 9 deletions src/audiosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,21 @@ BestAudioSource::BestAudioSource(const std::filesystem::path &SourceFile, int Tr
if (LAVFOpts)
LAVFOptions = *LAVFOpts;

if (CacheMode < 0 || CacheMode > 2)
throw BestSourceException("CacheMode must be between 0 and 2");
if (CacheMode < 0 || CacheMode > 4)
throw BestSourceException("CacheMode must be between 0 and 4");

std::unique_ptr<LWAudioDecoder> Decoder(new LWAudioDecoder(Source, AudioTrack, VariableFormat, Threads, LAVFOptions, DrcScale));

Decoder->GetAudioProperties(AP);
AudioTrack = Decoder->GetTrack();
FileSize = Decoder->GetSourceSize();

if (CacheMode == bcmDisable || !ReadAudioTrackIndex(CachePath)) {
if (CacheMode == bcmDisable || !ReadAudioTrackIndex(IsAbsolutePathCacheMode(CacheMode), CachePath)) {
if (!IndexTrack(Progress))
throw BestSourceException("Indexing of '" + Source.u8string() + "' track #" + std::to_string(AudioTrack) + " failed");

if (CacheMode == bcmAlwaysWrite || (CacheMode == bcmAuto && TrackIndex.Frames.size() >= 100)) {
if (!WriteAudioTrackIndex(CachePath))
if (ShouldWriteIndex(CacheMode, TrackIndex.Frames.size())) {
if (!WriteAudioTrackIndex(IsAbsolutePathCacheMode(CacheMode), CachePath))
throw BestSourceException("Failed to write index to '" + CachePath.u8string() + "' for track #" + std::to_string(AudioTrack));
}
}
Expand Down Expand Up @@ -1063,8 +1063,8 @@ static AudioCompArray GetAudioCompArray(int64_t PTS, int64_t Length) {
return Result;
}

bool BestAudioSource::WriteAudioTrackIndex(const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(CachePath, Source, AudioTrack, true);
bool BestAudioSource::WriteAudioTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(AbsolutePath, CachePath, Source, AudioTrack, true);
if (!F)
return false;
WriteBSHeader(F, false);
Expand Down Expand Up @@ -1137,8 +1137,8 @@ bool BestAudioSource::WriteAudioTrackIndex(const std::filesystem::path &CachePat
return true;
}

bool BestAudioSource::ReadAudioTrackIndex(const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(CachePath, Source, AudioTrack, false);
bool BestAudioSource::ReadAudioTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(AbsolutePath, CachePath, Source, AudioTrack, false);
if (!F)
return false;
if (!ReadBSHeader(F, false))
Expand Down
4 changes: 2 additions & 2 deletions src/audiosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class BestAudioSource {
std::vector<FrameInfo> Frames;
};

bool WriteAudioTrackIndex(const std::filesystem::path &CachePath);
bool ReadAudioTrackIndex(const std::filesystem::path &CachePath);
bool WriteAudioTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath);
bool ReadAudioTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath);

class Cache {
private:
Expand Down
20 changes: 17 additions & 3 deletions src/bsshared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ void BSDebugPrint(const std::string_view Message, int64_t RequestedN, int64_t Cu
}
}

static std::filesystem::path GetDefaultCachePath() {
bool ShouldWriteIndex(int CacheMode, size_t Frames) {
return (CacheMode == bcmAlwaysWriteSubTree || CacheMode == bcmAlwaysAbsolutePath) || ((CacheMode == bcmAutoSubTree || CacheMode == bcmAutoAbsolutePath) && Frames >= 100);
}

bool IsAbsolutePathCacheMode(int CacheMode) {
return (CacheMode == bcmAutoAbsolutePath || CacheMode == bcmAlwaysAbsolutePath);
}

static std::filesystem::path GetDefaultCacheSubTreePath() {
#ifdef _WIN32
std::vector<wchar_t> appDataBuffer(MAX_PATH + 1);
if (SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, appDataBuffer.data()) != S_OK)
Expand Down Expand Up @@ -121,8 +129,14 @@ file_ptr_t OpenNormalFile(const std::filesystem::path &Filename, bool Write) {
return F;
}

file_ptr_t OpenCacheFile(const std::filesystem::path &CacheBasePath, const std::filesystem::path &Source, int Track, bool Write) {
std::filesystem::path CacheFile = MangleCachePath(CacheBasePath.empty() ? GetDefaultCachePath() : CacheBasePath, Source);
file_ptr_t OpenCacheFile(bool AbsolutePath, const std::filesystem::path &CachePath, const std::filesystem::path &Source, int Track, bool Write) {
std::filesystem::path CacheFile;

if (AbsolutePath)
CacheFile = CachePath.empty() ? Source : CachePath;
else
CacheFile = MangleCachePath(CachePath.empty() ? GetDefaultCacheSubTreePath() : CachePath, Source);

CacheFile += "." + std::to_string(Track) + ".bsindex";
std::error_code ec;
std::filesystem::create_directories(CacheFile.parent_path(), ec);
Expand Down
13 changes: 10 additions & 3 deletions src/bsshared.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ typedef std::function<bool(int Track, int64_t Current, int64_t Total)> ProgressF

enum BestCacheMode {
bcmDisable = 0,
bcmAuto,
bcmAlwaysWrite
bcmAutoSubTree,
bcmAlwaysWriteSubTree,
bcmAutoAbsolutePath,
bcmAlwaysAbsolutePath,
bcmAuto [[deprecated]] = bcmAutoSubTree,
bcmAlwaysWrite [[deprecated]] = bcmAlwaysWriteSubTree
};

struct AVRational;
Expand All @@ -70,8 +74,11 @@ int SetFFmpegLogLevel(int Level);
void SetBSDebugOutput(bool DebugOutput);
void BSDebugPrint(const std::string_view Message, int64_t RequestedN = -1, int64_t CurrentN = -1);

bool ShouldWriteIndex(int CacheMode, size_t Frames);
bool IsAbsolutePathCacheMode(int CacheMode);

file_ptr_t OpenNormalFile(const std::filesystem::path &Filename, bool Write);
file_ptr_t OpenCacheFile(const std::filesystem::path &CacheBasePath, const std::filesystem::path &Source, int Track, bool Write);
file_ptr_t OpenCacheFile(bool AbsolutePath, const std::filesystem::path &CacheBasePath, const std::filesystem::path &Source, int Track, bool Write);
void WriteByte(file_ptr_t &F, uint8_t Value);
void WriteInt(file_ptr_t &F, int Value);
void WriteInt64(file_ptr_t &F, int64_t Value);
Expand Down
18 changes: 9 additions & 9 deletions src/videosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,21 +905,21 @@ BestVideoSource::BestVideoSource(const std::filesystem::path &SourceFile, const
if (ExtraHWFrames < 0)
throw BestSourceException("ExtraHWFrames must be 0 or greater");

if (CacheMode < 0 || CacheMode > 2)
throw BestSourceException("CacheMode must be between 0 and 2");
if (CacheMode < 0 || CacheMode > 4)
throw BestSourceException("CacheMode must be between 0 and 4");

std::unique_ptr<LWVideoDecoder> Decoder(new LWVideoDecoder(Source, HWDevice, ExtraHWFrames, VideoTrack, VariableFormat, Threads, LAVFOptions));

Decoder->GetVideoProperties(VP);
VideoTrack = Decoder->GetTrack();
FileSize = Decoder->GetSourceSize();

if (CacheMode == bcmDisable || !ReadVideoTrackIndex(CachePath)) {
if (CacheMode == bcmDisable || !ReadVideoTrackIndex(IsAbsolutePathCacheMode(CacheMode), CachePath)) {
if (!IndexTrack(Progress))
throw BestSourceException("Indexing of '" + Source.u8string() + "' track #" + std::to_string(VideoTrack) + " failed");

if (CacheMode == bcmAlwaysWrite || (CacheMode == bcmAuto && TrackIndex.Frames.size() >= 100)) {
if (!WriteVideoTrackIndex(CachePath))
if (ShouldWriteIndex(CacheMode, TrackIndex.Frames.size())) {
if (!WriteVideoTrackIndex(IsAbsolutePathCacheMode(CacheMode), CachePath))
throw BestSourceException("Failed to write index to '" + CachePath.u8string() + "' for track #" + std::to_string(VideoTrack));
}
}
Expand Down Expand Up @@ -1489,8 +1489,8 @@ static VideoCompArray GetVideoCompArray(int64_t PTS, int RepeatPict, bool KeyFra
return Result;
}

bool BestVideoSource::WriteVideoTrackIndex(const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(CachePath, Source, VideoTrack, true);
bool BestVideoSource::WriteVideoTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(AbsolutePath, CachePath, Source, VideoTrack, true);
if (!F)
return false;
WriteBSHeader(F, true);
Expand Down Expand Up @@ -1566,8 +1566,8 @@ bool BestVideoSource::WriteVideoTrackIndex(const std::filesystem::path &CachePat
return true;
}

bool BestVideoSource::ReadVideoTrackIndex(const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(CachePath, Source, VideoTrack, false);
bool BestVideoSource::ReadVideoTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath) {
file_ptr_t F = OpenCacheFile(AbsolutePath, CachePath, Source, VideoTrack, false);
if (!F)
return false;
if (!ReadBSHeader(F, true))
Expand Down
4 changes: 2 additions & 2 deletions src/videosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ class BestVideoSource {
std::vector<FrameInfo> Frames;
};

bool WriteVideoTrackIndex(const std::filesystem::path &CachePath);
bool ReadVideoTrackIndex(const std::filesystem::path &CachePath);
bool WriteVideoTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath);
bool ReadVideoTrackIndex(bool AbsolutePath, const std::filesystem::path &CachePath);

class Cache {
private:
Expand Down

0 comments on commit eea1517

Please sign in to comment.