Fix for new libdcp API.
[libsub.git] / src / stl_util.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "stl_util.h"
21 #include "exceptions.h"
22 #include "compose.hpp"
23 #include <string>
24 #include <cmath>
25
26 using std::string;
27 using namespace sub;
28
29 bool
30 about_equal (float a, float b)
31 {
32         return fabs (a - b) < 1e-4;
33 }
34
35 string
36 sub::stl_frame_rate_to_dfc (float r)
37 {
38         /* As requested by TJ in January 2018, and as apparently used by Annotation Edit in recent versions */
39         if (about_equal (r, 23.976)) {
40                 return "STL23.01";
41         } else if (about_equal (r, 24)) {
42                 return "STL24.01";
43         } else if (about_equal (r, 25)) {
44                 return "STL25.01";
45         } else if (about_equal (r, 30)) {
46                 return "STL30.01";
47         }
48
49         return "STL25.01";
50 }
51
52 float
53 sub::stl_dfc_to_frame_rate (string s)
54 {
55         if (s == "STL23.01") {
56                 return 23.976;
57         } else if (s == "STL24.01") {
58                 return 24;
59         } else if (s == "STL25.01") {
60                 return 25;
61         } else if (s == "STL30.01") {
62                 return 30;
63         }
64
65         throw STLError (String::compose ("Unknown disk format code %1 in binary STL file", s));
66 }