Extract frame info read/write to new class.
[dcpomatic.git] / src / lib / frame_info.cc
1 /*
2     Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "frame_info.h"
23 #include <string>
24
25
26 using std::shared_ptr;
27 using std::string;
28
29
30 J2KFrameInfo::J2KFrameInfo(uint64_t offset_, uint64_t size_, string hash_)
31         : dcp::J2KFrameInfo(offset_, size_, hash_)
32 {
33
34 }
35
36
37 J2KFrameInfo::J2KFrameInfo(dcp::J2KFrameInfo const& info)
38         : dcp::J2KFrameInfo(info)
39 {
40
41 }
42
43
44 J2KFrameInfo::J2KFrameInfo(shared_ptr<InfoFileHandle> info_file, Frame frame, Eyes eyes)
45 {
46         info_file->get().seek(position(frame, eyes), SEEK_SET);
47         info_file->get().checked_read(&offset, sizeof(offset));
48         info_file->get().checked_read(&size, sizeof(size));
49
50         char hash_buffer[33];
51         info_file->get().checked_read(hash_buffer, 32);
52         hash_buffer[32] = '\0';
53         hash = hash_buffer;
54 }
55
56
57 long
58 J2KFrameInfo::position(Frame frame, Eyes eyes) const
59 {
60         switch (eyes) {
61         case Eyes::BOTH:
62                 return frame * _size_on_disk;
63         case Eyes::LEFT:
64                 return frame * _size_on_disk * 2;
65         case Eyes::RIGHT:
66                 return frame * _size_on_disk * 2 + _size_on_disk;
67         default:
68                 DCPOMATIC_ASSERT(false);
69         }
70
71         DCPOMATIC_ASSERT(false);
72 }
73
74
75 /** @param frame reel-relative frame */
76 void
77 J2KFrameInfo::write(shared_ptr<InfoFileHandle> info_file, Frame frame, Eyes eyes) const
78 {
79         info_file->get().seek(position(frame, eyes), SEEK_SET);
80         info_file->get().checked_write(&offset, sizeof(offset));
81         info_file->get().checked_write(&size, sizeof(size));
82         info_file->get().checked_write(hash.c_str(), hash.size());
83 }
84