ReelEncryptableAsset -> ReelMXF.
[libdcp.git] / src / reel_picture_asset.cc
1 /*
2     Copyright (C) 2014-2015 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/reel_picture_asset.h
21  *  @brief ReelPictureAsset class.
22  */
23
24 #include "reel_picture_asset.h"
25 #include "picture_asset.h"
26 #include "dcp_assert.h"
27 #include "compose.hpp"
28 #include <libcxml/cxml.h>
29 #include <libxml++/libxml++.h>
30 #include <iomanip>
31
32 using std::bad_cast;
33 using std::string;
34 using std::stringstream;
35 using boost::shared_ptr;
36 using boost::dynamic_pointer_cast;
37 using namespace dcp;
38
39 ReelPictureAsset::ReelPictureAsset ()
40         : _frame_rate (Fraction (24, 1))
41         , _screen_aspect_ratio (Fraction (1998, 1080))
42 {
43
44 }
45
46 ReelPictureAsset::ReelPictureAsset (shared_ptr<PictureAsset> asset, int64_t entry_point)
47         : ReelAsset (asset, asset->edit_rate(), asset->intrinsic_duration(), entry_point)
48         , ReelMXF (asset->key_id())
49         , _frame_rate (asset->frame_rate ())
50         , _screen_aspect_ratio (asset->screen_aspect_ratio ())
51 {
52         
53 }
54
55 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
56         : ReelAsset (node)
57         , ReelMXF (node)
58 {
59         _frame_rate = Fraction (node->string_child ("FrameRate"));
60         try {
61                 _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
62         } catch (XMLError& e) {
63                 /* Maybe it's not a fraction */
64         }
65         try {
66                 float f = node->number_child<float> ("ScreenAspectRatio");
67                 _screen_aspect_ratio = Fraction (f * 1000, 1000);
68         } catch (bad_cast& e) {
69
70         }
71 }
72
73 void
74 ReelPictureAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
75 {
76         ReelAsset::write_to_cpl (node, standard);
77
78         /* Find <MainPicture> */
79         xmlpp::Node* mp = find_child (node, cpl_node_name ());
80         
81         mp->add_child ("FrameRate")->add_child_text (String::compose ("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
82         if (standard == INTEROP) {
83                 stringstream s;
84                 s << std::fixed << std::setprecision (2) << (float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator);
85                 mp->add_child ("ScreenAspectRatio")->add_child_text (s.str ());
86         } else {
87                 mp->add_child ("ScreenAspectRatio")->add_child_text (
88                         String::compose ("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
89                         );
90         }
91
92         if (key_id ()) {
93                 /* Find <Hash> */
94                 xmlpp::Node* hash = find_child (mp, "Hash");
95                 mp->add_child_before (hash, "KeyId")->add_child_text ("urn:uuid:" + key_id().get ());
96         }
97 }
98
99 string
100 ReelPictureAsset::key_type () const
101 {
102         return "MDIK";
103 }
104
105 bool
106 ReelPictureAsset::equals (shared_ptr<const ReelAsset> other, EqualityOptions opt, NoteHandler note) const
107 {
108         if (!ReelAsset::equals (other, opt, note)) {
109                 return false;
110         }
111         
112         shared_ptr<const ReelPictureAsset> rpa = dynamic_pointer_cast<const ReelPictureAsset> (other);
113         if (!rpa) {
114                 return false;
115         }
116
117         if (_frame_rate != rpa->_frame_rate) {
118                 note (DCP_ERROR, "frame rates differ in reel");
119                 return false;
120         }
121
122         if (_screen_aspect_ratio != rpa->_screen_aspect_ratio) {
123                 note (DCP_ERROR, "screen aspect ratios differ in reel");
124                 return false;
125         }
126
127         return true;
128 }