2 Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
20 /** @file src/reel_picture_asset.h
21 * @brief ReelPictureAsset class.
24 #include "reel_picture_asset.h"
25 #include "picture_asset.h"
26 #include "dcp_assert.h"
27 #include "raw_convert.h"
28 #include "compose.hpp"
29 #include <libcxml/cxml.h>
30 #include <libxml++/libxml++.h>
36 using std::stringstream;
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39 using boost::optional;
42 ReelPictureAsset::ReelPictureAsset ()
43 : _frame_rate (Fraction (24, 1))
44 , _screen_aspect_ratio (Fraction (1998, 1080))
49 ReelPictureAsset::ReelPictureAsset (shared_ptr<PictureAsset> asset, int64_t entry_point)
50 : ReelAsset (asset, asset->edit_rate(), asset->intrinsic_duration(), entry_point)
51 , ReelMXF (asset->key_id())
52 , _frame_rate (asset->frame_rate ())
53 , _screen_aspect_ratio (asset->screen_aspect_ratio ())
58 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
62 _frame_rate = Fraction (node->string_child ("FrameRate"));
64 _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
65 } catch (XMLError& e) {
66 /* It's not a fraction */
68 float f = node->number_child<float> ("ScreenAspectRatio");
69 _screen_aspect_ratio = Fraction (f * 1000, 1000);
70 } catch (bad_cast& e) {
77 ReelPictureAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
79 ReelAsset::write_to_cpl (node, standard);
81 /* Find <MainPicture> */
82 xmlpp::Node* mp = find_child (node, cpl_node_name ());
84 mp->add_child ("FrameRate")->add_child_text (String::compose ("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
85 if (standard == INTEROP) {
87 /* Allowed values for this tag from the standard */
88 float allowed[] = { 1.33, 1.66, 1.77, 1.85, 2.00, 2.39 };
89 int const num_allowed = sizeof(allowed) / sizeof(float);
92 float ratio = float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator;
94 /* Pick the closest and use that */
95 optional<float> closest;
96 optional<float> error;
97 for (int i = 0; i < num_allowed; ++i) {
98 float const e = fabsf (allowed[i] - ratio);
99 if (!closest || e < error.get()) {
100 closest = allowed[i];
105 mp->add_child ("ScreenAspectRatio")->add_child_text (raw_convert<string> (closest.get(), 2, true));
107 mp->add_child ("ScreenAspectRatio")->add_child_text (
108 String::compose ("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
114 xmlpp::Node* hash = find_child (mp, "Hash");
115 mp->add_child_before (hash, "KeyId")->add_child_text ("urn:uuid:" + key_id().get ());
120 ReelPictureAsset::key_type () const
126 ReelPictureAsset::equals (shared_ptr<const ReelAsset> other, EqualityOptions opt, NoteHandler note) const
128 if (!ReelAsset::equals (other, opt, note)) {
132 shared_ptr<const ReelPictureAsset> rpa = dynamic_pointer_cast<const ReelPictureAsset> (other);
137 if (_frame_rate != rpa->_frame_rate) {
138 note (DCP_ERROR, "frame rates differ in reel");
142 if (_screen_aspect_ratio != rpa->_screen_aspect_ratio) {
143 note (DCP_ERROR, "screen aspect ratios differ in reel");