05b71f2e5c07d0d5798f83c9ac3765f1d426fd5a
[dcpomatic.git] / src / lib / format.cc
1 /*
2     Copyright (C) 2012 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 /** @file src/format.cc
21  *  @brief Class to describe a format (aspect ratio) that a Film should
22  *  be shown in.
23  */
24
25 #include <sstream>
26 #include <cstdlib>
27 #include <cassert>
28 #include <iomanip>
29 #include <iostream>
30 #include "format.h"
31 #include "film.h"
32 #include "playlist.h"
33
34 #include "i18n.h"
35
36 using std::string;
37 using std::setprecision;
38 using std::stringstream;
39 using std::vector;
40 using boost::shared_ptr;
41 using libdcp::Size;
42
43 vector<Format const *> Format::_formats;
44
45 /** @return A name to be presented to the user */
46 string
47 FixedFormat::name () const
48 {
49         stringstream s;
50         if (!_nickname.empty ()) {
51                 s << _nickname << N_(" (");
52         }
53
54         s << setprecision(3) << (_ratio / 100.0) << N_(":1");
55
56         if (!_nickname.empty ()) {
57                 s << N_(")");
58         }
59
60         return s.str ();
61 }
62
63 /** @return Identifier for this format as metadata for a Film's metadata file */
64 string
65 Format::as_metadata () const
66 {
67         return _id;
68 }
69
70 /** Fill our _formats vector with all available formats */
71 void
72 Format::setup_formats ()
73 {
74         /// TRANSLATORS: these are film picture aspect ratios; "Academy" means 1.37, "Flat" 1.85 and "Scope" 2.39.
75         _formats.push_back (
76                 new FixedFormat (119, libdcp::Size (1285, 1080), N_("119"), _("1.19"), N_("F"),
77                                  _("Source scaled to 1.19:1")
78                         ));
79         
80         _formats.push_back (
81                 new FixedFormat (133, libdcp::Size (1436, 1080), N_("133"), _("1.33"), N_("F"),
82                                  _("Source scaled to 1.33:1")
83                         ));
84         
85         _formats.push_back (
86                 new FixedFormat (138, libdcp::Size (1485, 1080), N_("138"), _("1.375"), N_("F"),
87                                  _("Source scaled to 1.375:1")
88                         ));
89         
90         _formats.push_back (
91                 new FixedFormat (133, libdcp::Size (1998, 1080), N_("133-in-flat"), _("4:3 within Flat"), N_("F"),
92                                  _("Source scaled to 1.33:1 then pillarboxed to Flat")
93                         ));
94         
95         _formats.push_back (
96                 new FixedFormat (137, libdcp::Size (1480, 1080), N_("137"), _("Academy"), N_("F"),
97                                  _("Source scaled to 1.37:1 (Academy ratio)")
98                         ));
99         
100         _formats.push_back (
101                 new FixedFormat (166, libdcp::Size (1793, 1080), N_("166"), _("1.66"), N_("F"),
102                                  _("Source scaled to 1.66:1")
103                         ));
104         
105         _formats.push_back (
106                 new FixedFormat (166, libdcp::Size (1998, 1080), N_("166-in-flat"), _("1.66 within Flat"), N_("F"),
107                                  _("Source scaled to 1.66:1 then pillarboxed to Flat")
108                         ));
109         
110         _formats.push_back (
111                 new FixedFormat (178, libdcp::Size (1998, 1080), N_("178-in-flat"), _("16:9 within Flat"), N_("F"),
112                                  _("Source scaled to 1.78:1 then pillarboxed to Flat")
113                         ));
114         
115         _formats.push_back (
116                 new FixedFormat (178, libdcp::Size (1920, 1080), N_("178"), _("16:9"), N_("F"),
117                                  _("Source scaled to 1.78:1")
118                         ));
119         
120         _formats.push_back (
121                 new FixedFormat (185, libdcp::Size (1998, 1080), N_("185"), _("Flat"), N_("F"),
122                                  _("Source scaled to Flat (1.85:1)")
123                         ));
124         
125         _formats.push_back (
126                 new FixedFormat (239, libdcp::Size (2048, 858), N_("239"), _("Scope"), N_("S"),
127                                  _("Source scaled to Scope (2.39:1)")
128                         ));
129                 
130         _formats.push_back (
131                 new VariableFormat (libdcp::Size (1998, 1080), N_("var-185"), _("Flat without stretch"), N_("F"),
132                                     _("Source scaled to fit Flat preserving its aspect ratio")
133                         ));
134         
135         _formats.push_back (
136                 new VariableFormat (libdcp::Size (2048, 858), N_("var-239"), _("Scope without stretch"), N_("S"),
137                                     _("Source scaled to fit Scope preserving its aspect ratio")
138                         ));
139 }
140
141 /** @param n Nickname.
142  *  @return Matching Format, or 0.
143  */
144 Format const *
145 Format::from_nickname (string n)
146 {
147         vector<Format const *>::iterator i = _formats.begin ();
148         while (i != _formats.end() && (*i)->nickname() != n) {
149                 ++i;
150         }
151
152         if (i == _formats.end ()) {
153                 return 0;
154         }
155
156         return *i;
157 }
158
159 /** @param i Id.
160  *  @return Matching Format, or 0.
161  */
162 Format const *
163 Format::from_id (string i)
164 {
165         vector<Format const *>::iterator j = _formats.begin ();
166         while (j != _formats.end() && (*j)->id() != i) {
167                 ++j;
168         }
169
170         if (j == _formats.end ()) {
171                 return 0;
172         }
173
174         return *j;
175 }
176
177
178 /** @param m Metadata, as returned from as_metadata().
179  *  @return Matching Format, or 0.
180  */
181 Format const *
182 Format::from_metadata (string m)
183 {
184         return from_id (m);
185 }
186
187 /** @return All available formats */
188 vector<Format const *>
189 Format::all ()
190 {
191         return _formats;
192 }
193
194 /** @param r Ratio multiplied by 100 (e.g. 185)
195  *  @param dcp Size (in pixels) of the images that we should put in a DCP.
196  *  @param id ID (e.g. 185)
197  *  @param n Nick name (e.g. Flat)
198  */
199 FixedFormat::FixedFormat (int r, libdcp::Size dcp, string id, string n, string d, string e)
200         : Format (dcp, id, n, d, e)
201         , _ratio (r)
202 {
203
204 }
205
206 /** @return Number of pixels (int the DCP image) to pad either side of the film
207  *  (so there are dcp_padding() pixels on the left and dcp_padding() on the right)
208  */
209 int
210 Format::dcp_padding (shared_ptr<const Playlist> p) const
211 {
212         int pad = rint ((_dcp_size.width - (_dcp_size.height * ratio_as_integer(p) / 100.0)) / 2.0);
213
214         /* This comes out -ve for Scope; bodge it */
215         if (pad < 0) {
216                 pad = 0;
217         }
218         
219         return pad;
220 }
221
222 float
223 Format::container_ratio_as_float () const
224 {
225         return static_cast<float> (_dcp_size.width) / _dcp_size.height;
226 }
227
228 VariableFormat::VariableFormat (libdcp::Size dcp, string id, string n, string d, string e)
229         : Format (dcp, id, n, d, e)
230 {
231
232 }
233
234 int
235 VariableFormat::ratio_as_integer (shared_ptr<const Playlist> p) const
236 {
237         return rint (ratio_as_float (p) * 100);
238 }
239
240 float
241 VariableFormat::ratio_as_float (shared_ptr<const Playlist> p) const
242 {
243         return float (p->video_size().width) / p->video_size().height;
244 }
245
246 /** @return A name to be presented to the user */
247 string
248 VariableFormat::name () const
249 {
250         return _nickname;
251 }