]> git.sesse.net Git - nageru/blob - futatabi/frame_on_disk.h
dbe121113f185bb9011ce3c58c9d0a835a5daa0c
[nageru] / futatabi / frame_on_disk.h
1 #ifndef _FRAME_ON_DISK_H
2 #define _FRAME_ON_DISK_H 1
3
4 #include "defs.h"
5
6 #include <algorithm>
7 #include <mutex>
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
11
12 extern std::mutex frame_mu;
13 struct FrameOnDisk {
14         int64_t pts = -1;  // -1 means empty.
15         off_t offset;
16         unsigned filename_idx;
17         uint32_t size;  // Not using size_t saves a few bytes; we can have so many frames. TODO: Not anymore due to audio_size.
18         uint32_t audio_size;
19         // Unfortunately, 32 bits wasted in padding here.
20 };
21 extern std::vector<FrameOnDisk> frames[MAX_STREAMS];  // Under frame_mu.
22 extern std::vector<std::string> frame_filenames;  // Under frame_mu.
23
24 static bool inline operator==(const FrameOnDisk &a, const FrameOnDisk &b)
25 {
26         return a.pts == b.pts &&
27                 a.offset == b.offset &&
28                 a.filename_idx == b.filename_idx &&
29                 a.size == b.size &&
30                 a.audio_size == b.audio_size;
31 }
32
33 // A helper class to read frames from disk. It caches the file descriptor
34 // so that the kernel has a better chance of doing readahead when it sees
35 // the sequential reads. (For this reason, each display has a private
36 // FrameReader. Thus, we can easily keep multiple open file descriptors around
37 // for a single .frames file.)
38 //
39 // Thread-compatible, but not thread-safe.
40 class FrameReader {
41 public:
42         FrameReader();
43         ~FrameReader();
44         std::string read_frame(FrameOnDisk frame);
45
46 private:
47         int fd = -1;
48         int last_filename_idx = -1;
49 };
50
51 // Utility functions for dealing with binary search.
52 inline std::vector<FrameOnDisk>::iterator
53 find_last_frame_before(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
54 {
55         return std::lower_bound(frames.begin(), frames.end(), pts_origin,
56                                 [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
57 }
58
59 inline std::vector<FrameOnDisk>::iterator
60 find_first_frame_at_or_after(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
61 {
62         return std::upper_bound(frames.begin(), frames.end(), pts_origin - 1,
63                                 [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; });
64 }
65
66 #endif  // !defined(_FRAME_ON_DISK_H)