Purge assert() from src/, at least (not asdcplib).
[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 /** @file  src/reel_picture_asset.h
21  *  @brief ReelPictureAsset class.
22  */
23
24 #include "content.h"
25 #include "reel_picture_asset.h"
26 #include "picture_mxf.h"
27 #include "dcp_assert.h"
28 #include "compose.hpp"
29 #include <libcxml/cxml.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<PictureMXF> content, int64_t entry_point)
47         : ReelMXFAsset (content, content->edit_rate(), content->intrinsic_duration(), entry_point)
48         , _frame_rate (content->frame_rate ())
49         , _screen_aspect_ratio (content->screen_aspect_ratio ())
50 {
51         
52 }
53
54 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
55         : ReelMXFAsset (node)
56 {
57         _frame_rate = Fraction (node->string_child ("FrameRate"));
58         try {
59                 _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
60         } catch (XMLError& e) {
61                 /* Maybe it's not a fraction */
62         }
63         try {
64                 float f = node->number_child<float> ("ScreenAspectRatio");
65                 _screen_aspect_ratio = Fraction (f * 1000, 1000);
66         } catch (bad_cast& e) {
67
68         }
69 }
70
71 void
72 ReelPictureAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
73 {
74         ReelMXFAsset::write_to_cpl (node, standard);
75
76         xmlpp::Node::NodeList c = node->get_children ();
77         xmlpp::Node::NodeList::iterator i = c.begin();
78         while (i != c.end() && (*i)->get_name() != cpl_node_name ()) {
79                 ++i;
80         }
81
82         DCP_ASSERT (i != c.end ());
83         
84         (*i)->add_child ("FrameRate")->add_child_text (String::compose ("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
85         if (standard == INTEROP) {
86                 stringstream s;
87                 s << std::fixed << std::setprecision (2) << (float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator);
88                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (s.str ());
89         } else {
90                 (*i)->add_child ("ScreenAspectRatio")->add_child_text (
91                         String::compose ("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
92                         );
93         }
94 }
95
96 string
97 ReelPictureAsset::key_type () const
98 {
99         return "MDIK";
100 }
101
102 bool
103 ReelPictureAsset::equals (shared_ptr<const ReelAsset> other, EqualityOptions opt, boost::function<void (NoteType, std::string)> note) const
104 {
105         if (!ReelAsset::equals (other, opt, note)) {
106                 return false;
107         }
108         
109         shared_ptr<const ReelPictureAsset> rpa = dynamic_pointer_cast<const ReelPictureAsset> (other);
110         if (!rpa) {
111                 return false;
112         }
113
114         if (_frame_rate != rpa->_frame_rate) {
115                 note (DCP_ERROR, "frame rates differ in reel");
116                 return false;
117         }
118
119         if (_screen_aspect_ratio != rpa->_screen_aspect_ratio) {
120                 note (DCP_ERROR, "screen aspect ratios differ in reel");
121                 return false;
122         }
123
124         return true;
125 }