X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=futatabi%2Fframe_on_disk.h;h=f74cb86f717161a9b6a9ffab8575594a5a8548b4;hb=b3a7a9eddd8b800f1b921332e8fdacb72e0d9b20;hp=184385792528130df7e3ba16637385c0c14a35db;hpb=6e116a6bbeb2c047a3bfb084395ec601ce211e6c;p=nageru diff --git a/futatabi/frame_on_disk.h b/futatabi/frame_on_disk.h index 1843857..f74cb86 100644 --- a/futatabi/frame_on_disk.h +++ b/futatabi/frame_on_disk.h @@ -1,24 +1,32 @@ #ifndef _FRAME_ON_DISK_H #define _FRAME_ON_DISK_H 1 +#include "defs.h" + +#include #include +#include #include #include -#include - -#include "defs.h" - extern std::mutex frame_mu; struct FrameOnDisk { - int64_t pts = -1; // -1 means empty. - off_t offset; - unsigned filename_idx; - uint32_t size; // Not using size_t saves a few bytes; we can have so many frames. + int64_t pts = -1; // -1 means empty. + off_t offset; + unsigned filename_idx; + uint32_t size; // Not using size_t saves a few bytes; we can have so many frames. }; extern std::vector frames[MAX_STREAMS]; // Under frame_mu. extern std::vector frame_filenames; // Under frame_mu. +static bool inline operator==(const FrameOnDisk &a, const FrameOnDisk &b) +{ + return a.pts == b.pts && + a.offset == b.offset && + a.filename_idx == b.filename_idx && + a.size == b.size; +} + // A helper class to read frames from disk. It caches the file descriptor // so that the kernel has a better chance of doing readahead when it sees // the sequential reads. (For this reason, each display has a private @@ -26,6 +34,7 @@ extern std::vector frame_filenames; // Under frame_mu. // for a single .frames file.) class FrameReader { public: + FrameReader(); ~FrameReader(); std::string read_frame(FrameOnDisk frame); @@ -34,4 +43,19 @@ private: int last_filename_idx = -1; }; +// Utility functions for dealing with binary search. +inline std::vector::iterator +find_last_frame_before(std::vector &frames, int64_t pts_origin) +{ + return std::lower_bound(frames.begin(), frames.end(), pts_origin, + [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; }); +} + +inline std::vector::iterator +find_first_frame_at_or_after(std::vector &frames, int64_t pts_origin) +{ + return std::upper_bound(frames.begin(), frames.end(), pts_origin - 1, + [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; }); +} + #endif // !defined(_FRAME_ON_DISK_H)