1 /*
2    mkvmerge -- utility for splicing together matroska files
3    from component media subtypes
4 
5    Distributed under the GPL v2
6    see the file COPYING for details
7    or visit https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8 
9    class definition for the timestamp calculator
10 
11    Written by Moritz Bunkus <moritz@bunkus.org>.
12 */
13 
14 #pragma once
15 
16 #include "common/common_pch.h"
17 
18 #include "common/debugging.h"
19 #include "common/samples_to_timestamp_converter.h"
20 #include "common/timestamp.h"
21 
22 class packet_t;
23 using packet_cptr = std::shared_ptr<packet_t>;
24 
25 class timestamp_calculator_c {
26 private:
27   std::deque<std::pair<timestamp_c, std::optional<uint64_t>>> m_available_timestamps;
28   timestamp_c m_reference_timestamp, m_last_timestamp_returned;
29   int64_t m_samples_per_second, m_samples_since_reference_timestamp;
30   samples_to_timestamp_converter_c m_samples_to_timestamp;
31   bool m_allow_smaller_timestamps;
32   debugging_option_c m_debug;
33 
34 public:
35   timestamp_calculator_c(int64_t samples_per_second);
36 
37   void add_timestamp(std::optional<int64_t> const &timestamp, std::optional<uint64_t> stream_position = std::nullopt);
38   void add_timestamp(timestamp_c const &timestamp, std::optional<uint64_t> stream_position = std::nullopt);
39   void add_timestamp(int64_t timestamp, std::optional<uint64_t> stream_position = std::nullopt);
40   void add_timestamp(packet_cptr const &packet, std::optional<uint64_t> stream_position = std::nullopt);
41 
42   void drop_timestamps_before_position(uint64_t stream_position);
43 
44   timestamp_c get_next_timestamp(int64_t samples_in_frame, std::optional<uint64_t> stream_position = std::nullopt);
45   timestamp_c get_duration(int64_t samples);
46 
47   void set_samples_per_second(int64_t samples_per_second);
48   void set_allow_smaller_timestamps(bool allow);
49 
50 protected:
51   timestamp_c fetch_next_available_timestamp(int64_t samples_in_frame);
52   timestamp_c calculate_next_timestamp(int64_t samples_in_frame);
53 };
54