libdcp
reel_picture_asset.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3 
4  This file is part of libdcp.
5 
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.
10 
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.
15 
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/>.
18 
19  In addition, as a special exception, the copyright holders give
20  permission to link the code of portions of this program with the
21  OpenSSL library under certain conditions as described in each
22  individual source file, and distribute linked combinations
23  including the two.
24 
25  You must obey the GNU General Public License in all respects
26  for all of the code used other than OpenSSL. If you modify
27  file(s) with this exception, you may extend this exception to your
28  version of the file(s), but you are not obligated to do so. If you
29  do not wish to do so, delete this exception statement from your
30  version. If you delete this exception statement from all source
31  files in the program, then also delete it here.
32 */
33 
34 
40 #include "compose.hpp"
41 #include "dcp_assert.h"
42 #include "j2k_picture_asset.h"
43 #include "raw_convert.h"
44 #include "reel_picture_asset.h"
45 #include "warnings.h"
46 #include <libcxml/cxml.h>
47 LIBDCP_DISABLE_WARNINGS
48 #include <libxml++/libxml++.h>
49 LIBDCP_ENABLE_WARNINGS
50 #include <iomanip>
51 #include <cmath>
52 
53 
54 using std::bad_cast;
55 using std::string;
56 using std::shared_ptr;
57 using std::dynamic_pointer_cast;
58 using boost::optional;
59 using namespace dcp;
60 
61 
62 ReelPictureAsset::ReelPictureAsset(shared_ptr<PictureAsset> asset, int64_t entry_point)
63  : ReelFileAsset (asset, asset->key_id(), asset->id(), asset->edit_rate(), asset->intrinsic_duration(), entry_point)
64  , _frame_rate (asset->frame_rate ())
65  , _screen_aspect_ratio (asset->screen_aspect_ratio ())
66 {
67 
68 }
69 
70 
71 ReelPictureAsset::ReelPictureAsset (shared_ptr<const cxml::Node> node)
72  : ReelFileAsset (node)
73 {
74  _frame_rate = Fraction (node->string_child ("FrameRate"));
75  try {
76  _screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
77  } catch (XMLError& e) {
78  /* It's not a fraction */
79  try {
80  float f = node->number_child<float> ("ScreenAspectRatio");
81  _screen_aspect_ratio = Fraction (f * 1000, 1000);
82  } catch (bad_cast& e) {
83 
84  }
85  }
86 }
87 
88 
89 xmlpp::Element*
90 ReelPictureAsset::write_to_cpl(xmlpp::Element* node, Standard standard) const
91 {
92  auto asset = ReelFileAsset::write_to_cpl (node, standard);
93 
94  cxml::add_text_child(asset, "FrameRate", String::compose("%1 %2", _frame_rate.numerator, _frame_rate.denominator));
95 
96  if (standard == Standard::INTEROP) {
97 
98  /* Allowed values for this tag from the standard */
99  float allowed[] = { 1.33, 1.66, 1.77, 1.85, 2.00, 2.39 };
100  int const num_allowed = sizeof(allowed) / sizeof(float);
101 
102  /* Actual ratio */
103  float ratio = float (_screen_aspect_ratio.numerator) / _screen_aspect_ratio.denominator;
104 
105  /* Pick the closest and use that */
106  optional<float> closest;
107  optional<float> error;
108  for (int i = 0; i < num_allowed; ++i) {
109  float const e = fabsf (allowed[i] - ratio);
110  if (!closest || e < error.get()) {
111  closest = allowed[i];
112  error = e;
113  }
114  }
115 
116  cxml::add_text_child(asset, "ScreenAspectRatio", raw_convert<string>(closest.get(), 2, true));
117  } else {
118  cxml::add_text_child(
119  asset,
120  "ScreenAspectRatio",
121  String::compose("%1 %2", _screen_aspect_ratio.numerator, _screen_aspect_ratio.denominator)
122  );
123  }
124 
125  return asset;
126 }
127 
128 
129 bool
130 ReelPictureAsset::equals(shared_ptr<const ReelPictureAsset> other, EqualityOptions const& opt, NoteHandler note) const
131 {
132  if (!asset_equals (other, opt, note)) {
133  return false;
134  }
135  if (!file_asset_equals (other, opt, note)) {
136  return false;
137  }
138 
139  auto rpa = dynamic_pointer_cast<const ReelPictureAsset>(other);
140  if (!rpa) {
141  return false;
142  }
143 
144  if (_frame_rate != rpa->_frame_rate) {
145  note (NoteType::ERROR, "frame rates differ in reel");
146  return false;
147  }
148 
149  if (_screen_aspect_ratio != rpa->_screen_aspect_ratio) {
150  note (NoteType::ERROR, "screen aspect ratios differ in reel");
151  return false;
152  }
153 
154  return true;
155 }
A class to describe what "equality" means for a particular test.
A fraction (i.e. a thing with an integer numerator and an integer denominator).
Definition: types.h:168
std::shared_ptr< const PictureAsset > asset() const
An XML error.
Definition: exceptions.h:191
DCP_ASSERT macro.
J2KPictureAsset class.
Namespace for everything in libdcp.
Definition: array_data.h:50
Methods for conversion to/from string.
ReelPictureAsset class.