9fdc3b9e549ee6bc3dc20f78c824d0415b8be44d
[libdcp.git] / src / reel_picture_asset.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 "content.h"
21 #include "reel_picture_asset.h"
22 #include "picture_mxf.h"
23 #include "compose.hpp"
24 #include <libcxml/cxml.h>
25
26 using std::bad_cast;
27 using std::string;
28 using std::stringstream;
29 using boost::shared_ptr;
30 using boost::lexical_cast;
31 using namespace dcp;
32
33 ReelPictureAsset::ReelPictureAsset ()
34         : _frame_rate (Fraction (24, 1))
35         , _screen_aspect_ratio (Fraction (1998, 1080))
36 {
37
38 }
39
40 ReelPictureAsset::ReelPictureAsset (shared_ptr<PictureMXF> content, int64_t entry_point)
41         : ReelAsset (content, entry_point)
42         , _frame_rate (content->frame_rate ())
43         , _screen_aspect_ratio (content->screen_aspect_ratio ())
44 {
45         
46 }
47
48 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
49         : ReelAsset (node)
50 {
51         _frame_rate = Fraction (node->string_child ("FrameRate"));
52         try {
53                 _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
54         } catch (XMLError& e) {
55                 /* Maybe it's not a fraction */
56         }
57         try {
58                 float f = node->number_child<float> ("ScreenAspectRatio");
59                 _screen_aspect_ratio = Fraction (f * 1000, 1000);
60         } catch (bad_cast& e) {
61
62         }
63 }
64
65 void
66 ReelPictureAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
67 {
68         ReelAsset::write_to_cpl (node, standard);
69
70         xmlpp::Node::NodeList c = node->get_children ();
71         xmlpp::Node::NodeList::iterator i = c.begin();
72         while (i != c.end() && (*i)->get_name() != cpl_node_name ()) {
73                 ++i;
74         }
75
76         assert (i != c.end ());
77         
78         (*i)->add_child ("FrameRate")->add_child_text (String::compose ("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
79         if (standard == INTEROP) {
80                 stringstream s;
81                 s << std::fixed << std::setprecision (2) << (float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator);
82                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (s.str ());
83         } else {
84                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (
85                         String::compose ("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
86                         );
87         }
88 }