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    the timestamp factory
10 
11    Written by Moritz Bunkus <moritz@bunkus.org>.
12    Modifications by Steve Lhomme <steve.lhomme@free.fr>.
13 */
14 
15 #include "common/common_pch.h"
16 
17 #include <QRegularExpression>
18 
19 #include "common/mm_file_io.h"
20 #include "common/mm_proxy_io.h"
21 #include "common/mm_text_io.h"
22 #include "common/qt.h"
23 #include "common/strings/formatting.h"
24 #include "common/strings/parsing.h"
25 #include "merge/timestamp_factory.h"
26 
27 timestamp_factory_cptr
create(std::string const & file_name,std::string const & source_name,int64_t tid)28 timestamp_factory_c::create(std::string const &file_name,
29                             std::string const &source_name,
30                             int64_t tid) {
31   if (file_name.empty())
32     return timestamp_factory_cptr{};
33 
34   mm_io_cptr in;
35   try {
36     in = std::make_shared<mm_text_io_c>(std::make_shared<mm_file_io_c>(file_name));
37   } catch(...) {
38     mxerror(fmt::format(Y("The timestamp file '{0}' could not be opened for reading.\n"), file_name));
39   }
40 
41   std::string line;
42   int version = -1;
43   bool ok     = in->getline2(line);
44   if (ok) {
45     auto format_line_re = QRegularExpression{"^# *time(?:code|stamp) *format v(\\d+).*"};
46     auto matches        = format_line_re.match(Q(line));
47 
48     if (matches.hasMatch())
49       ok = mtx::string::parse_number(to_utf8(matches.captured(1)), version);
50     else
51       ok = false;
52   }
53 
54   if (!ok)
55     mxerror(fmt::format(Y("The timestamp file '{0}' contains an unsupported/unrecognized format line. The very first line must look like '# timestamp format v1'.\n"),
56                         file_name));
57 
58   timestamp_factory_c *factory = nullptr; // avoid gcc warning
59   if (1 == version)
60     factory = new timestamp_factory_v1_c(file_name, source_name, tid);
61 
62   else if ((2 == version) || (4 == version))
63     factory = new timestamp_factory_v2_c(file_name, source_name, tid, version);
64 
65   else if (3 == version)
66     factory = new timestamp_factory_v3_c(file_name, source_name, tid);
67 
68   else
69     mxerror(fmt::format(Y("The timestamp file '{0}' contains an unsupported/unrecognized format (version {1}).\n"), file_name, version));
70 
71   factory->parse(*in);
72 
73   return timestamp_factory_cptr(factory);
74 }
75 
76 timestamp_factory_cptr
create_fps_factory(int64_t default_duration,timestamp_sync_t const & tcsync)77 timestamp_factory_c::create_fps_factory(int64_t default_duration,
78                                         timestamp_sync_t const &tcsync) {
79   return timestamp_factory_cptr{ new forced_default_duration_timestamp_factory_c{default_duration, tcsync, "", 1} };
80 }
81 
82 void
parse(mm_io_c & in)83 timestamp_factory_v1_c::parse(mm_io_c &in) {
84   std::string line;
85   timestamp_range_c t;
86   std::vector<timestamp_range_c>::iterator iit;
87   std::vector<timestamp_range_c>::const_iterator pit;
88 
89   int line_no = 1;
90   do {
91     if (!in.getline2(line))
92       mxerror(fmt::format(Y("The timestamp file '{0}' does not contain a valid 'Assume' line with the default number of frames per second.\n"), m_file_name));
93     line_no++;
94     mtx::string::strip(line);
95     if (!line.empty() && ('#' != line[0]))
96       break;
97   } while (true);
98 
99   if (!balg::istarts_with(line, "assume "))
100     mxerror(fmt::format(Y("The timestamp file '{0}' does not contain a valid 'Assume' line with the default number of frames per second.\n"), m_file_name));
101 
102   line.erase(0, 6);
103   mtx::string::strip(line);
104 
105   if (!mtx::string::parse_number(line.c_str(), m_default_fps))
106     mxerror(fmt::format(Y("The timestamp file '{0}' does not contain a valid 'Assume' line with the default number of frames per second.\n"), m_file_name));
107 
108   while (in.getline2(line)) {
109     line_no++;
110     mtx::string::strip(line, true);
111     if (line.empty() || ('#' == line[0]))
112       continue;
113 
114     auto parts = mtx::string::split(line, ",", 3);
115     if (   (parts.size() != 3)
116         || !mtx::string::parse_number(parts[0], t.start_frame)
117         || !mtx::string::parse_number(parts[1], t.end_frame)
118         || !mtx::string::parse_number(parts[2], t.fps)) {
119       mxwarn(fmt::format(Y("Line {0} of the timestamp file '{1}' could not be parsed.\n"), line_no, m_file_name));
120       continue;
121     }
122 
123     if ((t.fps <= 0) || (t.end_frame < t.start_frame)) {
124       mxwarn(fmt::format(Y("Line {0} of the timestamp file '{1}' contains inconsistent data (e.g. the start frame number is bigger than the end frame "
125                            "number, or some values are smaller than zero).\n"), line_no, m_file_name));
126       continue;
127     }
128 
129     m_ranges.push_back(t);
130   }
131 
132   mxdebug_if(m_debug, fmt::format("ext_timestamps: Version 1, default fps {0}, {1} entries.\n", m_default_fps, m_ranges.size()));
133 
134   if (m_ranges.size() == 0)
135     t.start_frame = 0;
136   else {
137     std::sort(m_ranges.begin(), m_ranges.end());
138     bool done;
139     do {
140       done = true;
141       iit  = m_ranges.begin();
142       size_t i;
143       for (i = 0; i < (m_ranges.size() - 1); i++) {
144         iit++;
145         if (m_ranges[i].end_frame < (m_ranges[i + 1].start_frame - 1)) {
146           t.start_frame = m_ranges[i].end_frame + 1;
147           t.end_frame = m_ranges[i + 1].start_frame - 1;
148           t.fps = m_default_fps;
149           m_ranges.insert(iit, t);
150           done = false;
151           break;
152         }
153       }
154     } while (!done);
155     if (m_ranges[0].start_frame != 0) {
156       t.start_frame = 0;
157       t.end_frame = m_ranges[0].start_frame - 1;
158       t.fps = m_default_fps;
159       m_ranges.insert(m_ranges.begin(), t);
160     }
161     t.start_frame = m_ranges[m_ranges.size() - 1].end_frame + 1;
162   }
163 
164   t.end_frame = 0xfffffffffffffffll;
165   t.fps       = m_default_fps;
166   m_ranges.push_back(t);
167 
168   m_ranges[0].base_timestamp = 0.0;
169   pit = m_ranges.begin();
170   for (iit = m_ranges.begin() + 1; iit < m_ranges.end(); iit++, pit++)
171     iit->base_timestamp = pit->base_timestamp + ((double)pit->end_frame - (double)pit->start_frame + 1) * 1000000000.0 / pit->fps;
172 
173   for (iit = m_ranges.begin(); iit < m_ranges.end(); iit++)
174     mxdebug_if(m_debug, fmt::format("ranges: entry {0} -> {1} at {2} with {3}\n", iit->start_frame, iit->end_frame, iit->fps, iit->base_timestamp));
175 }
176 
177 bool
get_next(packet_t & packet)178 timestamp_factory_v1_c::get_next(packet_t &packet) {
179   packet.assigned_timestamp = get_at(m_frameno);
180   if (!m_preserve_duration || (0 >= packet.duration))
181     packet.duration = get_at(m_frameno + 1) - packet.assigned_timestamp;
182 
183   m_frameno++;
184   if ((m_frameno > m_ranges[m_current_range].end_frame) && (m_current_range < (m_ranges.size() - 1)))
185     m_current_range++;
186 
187   mxdebug_if(m_debug, fmt::format("ext_timestamps v1: tc {0} dur {1} for {2}\n", packet.assigned_timestamp, packet.duration, m_frameno - 1));
188 
189   return false;
190 }
191 
192 int64_t
get_at(uint64_t frame)193 timestamp_factory_v1_c::get_at(uint64_t frame) {
194   timestamp_range_c *t = &m_ranges[m_current_range];
195   if ((frame > t->end_frame) && (m_current_range < (m_ranges.size() - 1)))
196     t = &m_ranges[m_current_range + 1];
197 
198   return (int64_t)(t->base_timestamp + 1000000000.0 * (frame - t->start_frame) / t->fps);
199 }
200 
201 void
parse(mm_io_c & in)202 timestamp_factory_v2_c::parse(mm_io_c &in) {
203   std::string line;
204   std::map<int64_t, int64_t> dur_map;
205 
206   int64_t dur_sum          = 0;
207   int line_no              = 0;
208   double previous_timestamp = 0;
209 
210   while (in.getline2(line)) {
211     line_no++;
212     mtx::string::strip(line);
213     if ((line.length() == 0) || (line[0] == '#'))
214       continue;
215 
216     double timestamp;
217     if (!mtx::string::parse_number(line.c_str(), timestamp))
218       mxerror(fmt::format(Y("The line {0} of the timestamp file '{1}' does not contain a valid floating point number.\n"), line_no, m_file_name));
219 
220     if ((2 == m_version) && (timestamp < previous_timestamp))
221       mxerror(fmt::format(Y("The timestamp v2 file '{0}' contains timestamps that are not ordered. "
222                             "Due to a bug in mkvmerge versions up to and including v1.5.0 this was necessary "
223                             "if the track to which the timestamp file was applied contained B frames. "
224                             "Starting with v1.5.1 mkvmerge now handles this correctly, and the timestamps in the timestamp file must be ordered normally. "
225                             "For example, the frame sequence 'IPBBP...' at 25 FPS requires a timestamp file with "
226                             "the first timestamps being '0', '40', '80', '120' etc and. not '0', '120', '40', '80' etc.\n\n"
227                             "If you really have to specify non-sorted timestamps then use the timestamp format v4. "
228                             "It is identical to format v2 but allows non-sorted timestamps.\n"),
229                           in.get_file_name()));
230 
231     previous_timestamp = timestamp;
232     m_timestamps.push_back((int64_t)(timestamp * 1000000));
233     if (m_timestamps.size() > 1) {
234       int64_t duration = m_timestamps[m_timestamps.size() - 1] - m_timestamps[m_timestamps.size() - 2];
235       if (dur_map.find(duration) == dur_map.end())
236         dur_map[duration] = 1;
237       else
238         dur_map[duration] = dur_map[duration] + 1;
239       dur_sum += duration;
240       m_durations.push_back(duration);
241     }
242   }
243 
244   if (m_timestamps.empty())
245     mxerror(fmt::format(Y("The timestamp file '{0}' does not contain any valid entry.\n"), m_file_name));
246 
247   if (m_debug) {
248     mxdebug("Absolute probablities with maximum in separate line:\n");
249     mxdebug("Duration  | Absolute probability\n");
250     mxdebug("----------+---------------------\n");
251   }
252 
253   dur_sum = -1;
254   for (auto entry : dur_map) {
255     if ((0 > dur_sum) || (dur_map[dur_sum] < entry.second))
256       dur_sum = entry.first;
257     mxdebug_if(m_debug, fmt::format("{0: 9} | {1: 9}\n", entry.first, entry.second));
258   }
259 
260   mxdebug_if(m_debug, "Max-------+---------------------\n");
261   mxdebug_if(m_debug, fmt::format("{0: 9} | {1: 9}\n", dur_sum, dur_map[dur_sum]));
262 
263   if (0 < dur_sum)
264     m_default_duration = dur_sum;
265 
266   m_durations.push_back(dur_sum);
267 }
268 
269 bool
get_next(packet_t & packet)270 timestamp_factory_v2_c::get_next(packet_t &packet) {
271   if ((static_cast<size_t>(m_frameno) >= m_timestamps.size()) && !m_warning_printed) {
272     mxwarn_tid(m_source_name, m_tid,
273                fmt::format(Y("The number of external timestamps {0} is smaller than the number of frames in this track. "
274                              "The remaining frames of this track might not be timestamped the way you intended them to be. mkvmerge might even crash.\n"),
275                            m_timestamps.size()));
276     m_warning_printed = true;
277 
278     if (m_timestamps.empty()) {
279       packet.assigned_timestamp = 0;
280       if (!m_preserve_duration || (0 >= packet.duration))
281         packet.duration = 0;
282 
283     } else {
284       packet.assigned_timestamp = m_timestamps.back();
285       if (!m_preserve_duration || (0 >= packet.duration))
286         packet.duration = m_timestamps.back();
287     }
288 
289     return false;
290   }
291 
292   packet.assigned_timestamp = m_timestamps[m_frameno];
293   if (!m_preserve_duration || (0 >= packet.duration))
294     packet.duration = m_durations[m_frameno];
295   m_frameno++;
296 
297   return false;
298 }
299 
300 void
parse(mm_io_c & in)301 timestamp_factory_v3_c::parse(mm_io_c &in) {
302   std::string line;
303   timestamp_duration_c t;
304   std::vector<timestamp_duration_c>::iterator iit;
305 
306   std::string err_msg_assume = fmt::format(Y("The timestamp file '{0}' does not contain a valid 'Assume' line with the default number of frames per second.\n"), m_file_name);
307 
308   int line_no = 1;
309   do {
310     if (!in.getline2(line))
311       mxerror(err_msg_assume);
312     line_no++;
313     mtx::string::strip(line);
314     if ((line.length() != 0) && (line[0] != '#'))
315       break;
316   } while (true);
317 
318   if (!balg::istarts_with(line, "assume "))
319     mxerror(err_msg_assume);
320   line.erase(0, 6);
321   mtx::string::strip(line);
322 
323   if (!mtx::string::parse_number(line.c_str(), m_default_fps))
324     mxerror(err_msg_assume);
325 
326   while (in.getline2(line)) {
327     line_no++;
328     mtx::string::strip(line, true);
329     if ((line.length() == 0) || (line[0] == '#'))
330       continue;
331 
332     double dur;
333     if (balg::istarts_with(line, "gap,")) {
334       line.erase(0, 4);
335       mtx::string::strip(line);
336 
337       t.is_gap = true;
338       t.fps    = m_default_fps;
339 
340       if (!mtx::string::parse_number(line.c_str(), dur))
341         mxerror(fmt::format(Y("The timestamp file '{0}' does not contain a valid 'Gap' line with the duration of the gap.\n"), m_file_name));
342       t.duration = (int64_t)(1000000000.0 * dur);
343 
344     } else {
345       t.is_gap = false;
346       auto parts = mtx::string::split(line, ",");
347 
348       if ((1 == parts.size()) && mtx::string::parse_number(parts[0], dur))
349         t.fps = m_default_fps;
350 
351       else if ((2 != parts.size()) || !mtx::string::parse_number(parts[1], t.fps)) {
352         mxwarn(fmt::format(Y("Line {0} of the timestamp file '{1}' could not be parsed.\n"), line_no, m_file_name));
353         continue;
354       }
355       t.duration = (int64_t)(1000000000.0 * dur);
356     }
357 
358     if ((t.fps < 0) || (t.duration <= 0)) {
359       mxwarn(fmt::format(Y("Line {0} of the timestamp file '{1}' contains inconsistent data (e.g. the duration or the FPS are smaller than zero).\n"),
360                          line_no, m_file_name));
361       continue;
362     }
363 
364     m_durations.push_back(t);
365   }
366 
367   mxdebug_if(m_debug, fmt::format("ext_timestamps: Version 3, default fps {0}, {1} entries.\n", m_default_fps, m_durations.size()));
368 
369   if (m_durations.size() == 0)
370     mxwarn(fmt::format(Y("The timestamp file '{0}' does not contain any valid entry.\n"), m_file_name));
371 
372   t.duration = 0xfffffffffffffffll;
373   t.is_gap   = false;
374   t.fps      = m_default_fps;
375   m_durations.push_back(t);
376 
377   for (iit = m_durations.begin(); iit < m_durations.end(); iit++)
378     mxdebug_if(m_debug, fmt::format("durations:{0} entry for {1} with {2} FPS\n", iit->is_gap ? " gap" : "", iit->duration, iit->fps));
379 }
380 
381 bool
get_next(packet_t & packet)382 timestamp_factory_v3_c::get_next(packet_t &packet) {
383   bool result = false;
384 
385   if (m_durations[m_current_duration].is_gap) {
386     // find the next non-gap
387     size_t duration_index = m_current_duration;
388     while (m_durations[duration_index].is_gap || (0 == m_durations[duration_index].duration)) {
389       m_current_offset += m_durations[duration_index].duration;
390       duration_index++;
391     }
392     m_current_duration = duration_index;
393     result             = true;
394     // yes, there is a gap before this frame
395   }
396 
397   packet.assigned_timestamp = m_current_offset + m_current_timestamp;
398   // If default_fps is 0 then the duration is unchanged, usefull for audio.
399   if (m_durations[m_current_duration].fps && (!m_preserve_duration || (0 >= packet.duration)))
400     packet.duration = (int64_t)(1000000000.0 / m_durations[m_current_duration].fps);
401 
402   packet.duration   /= packet.time_factor;
403   m_current_timestamp += packet.duration;
404 
405   if (m_current_timestamp >= m_durations[m_current_duration].duration) {
406     m_current_offset   += m_durations[m_current_duration].duration;
407     m_current_timestamp  = 0;
408     m_current_duration++;
409   }
410 
411   mxdebug_if(m_debug, fmt::format("ext_timestamps v3: tc {0} dur {1}\n", packet.assigned_timestamp, packet.duration));
412 
413   return result;
414 }
415 
416 bool
get_next(packet_t & packet)417 forced_default_duration_timestamp_factory_c::get_next(packet_t &packet) {
418   packet.assigned_timestamp = m_frameno * m_default_duration + m_tcsync.displacement;
419   packet.duration           = m_default_duration;
420   ++m_frameno;
421 
422   return false;
423 }
424