Merge branch 'master' into 1.0
[libdcp.git] / src / parse / cpl.cc
1 /*
2     Copyright (C) 2012 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/cpl_file.cc
21  *  @brief Classes used to parse a CPL.
22  */
23
24 #include <iostream>
25 #include "cpl.h"
26 #include "../xml.h"
27 #include "../util.h"
28
29 using std::string;
30 using std::bad_cast;
31 using boost::shared_ptr;
32 using namespace dcp::parse;
33
34 CPL::CPL (string file)
35 {
36         cxml::Document f ("CompositionPlaylist");
37         f.read_file (file);
38         
39         id = f.string_child ("Id");
40         annotation_text = f.optional_string_child ("AnnotationText").get_value_or ("");
41         issue_date = f.string_child ("IssueDate");
42         creator = f.optional_string_child ("Creator").get_value_or ("");
43         content_title_text = f.string_child ("ContentTitleText");
44         content_kind = content_kind_from_string (f.string_child ("ContentKind"));
45         content_version = optional_type_child<ContentVersion> (f, "ContentVersion");
46         f.ignore_child ("RatingList");
47         reels = type_grand_children<Reel> (f, "ReelList", "Reel");
48
49         f.ignore_child ("Issuer");
50         f.ignore_child ("Signer");
51         f.ignore_child ("Signature");
52
53         f.done ();
54 }
55
56 ContentVersion::ContentVersion (shared_ptr<const cxml::Node> node)
57 {
58         id = node->optional_string_child ("Id").get_value_or ("");
59         label_text = node->string_child ("LabelText");
60         node->done ();
61 }
62
63 Reel::Reel (shared_ptr<const cxml::Node> node)
64 {
65         id = node->string_child ("Id");
66         asset_list = type_child<CPLAssetList> (node, "AssetList");
67
68         node->ignore_child ("AnnotationText");
69         node->done ();
70 }
71
72 CPLAssetList::CPLAssetList (shared_ptr<const cxml::Node> node)
73 {
74         main_picture = optional_type_child<MainPicture> (node, "MainPicture");
75         main_stereoscopic_picture = optional_type_child<MainStereoscopicPicture> (node, "MainStereoscopicPicture");
76         main_sound = optional_type_child<MainSound> (node, "MainSound");
77         main_subtitle = optional_type_child<MainSubtitle> (node, "MainSubtitle");
78
79         node->done ();
80 }
81
82 MainPicture::MainPicture (shared_ptr<const cxml::Node> node)
83         : Picture (node)
84 {
85
86 }
87
88 MainStereoscopicPicture::MainStereoscopicPicture (shared_ptr<const cxml::Node> node)
89         : Picture (node)
90 {
91
92 }
93
94 Picture::Picture (shared_ptr<const cxml::Node> node)
95 {
96         id = node->string_child ("Id");
97         annotation_text = node->optional_string_child ("AnnotationText").get_value_or ("");
98         edit_rate = Fraction (node->string_child ("EditRate"));
99         intrinsic_duration = node->number_child<int64_t> ("IntrinsicDuration");
100         entry_point = node->number_child<int64_t> ("EntryPoint");
101         duration = node->number_child<int64_t> ("Duration");
102         frame_rate = Fraction (node->string_child ("FrameRate"));
103         try {
104                 screen_aspect_ratio = Fraction (node->string_child ("ScreenAspectRatio"));
105         } catch (XMLError& e) {
106                 /* Maybe it's not a fraction */
107         }
108         try {
109                 float f = node->number_child<float> ("ScreenAspectRatio");
110                 screen_aspect_ratio = Fraction (f * 1000, 1000);
111         } catch (bad_cast& e) {
112
113         }
114
115         key_id = node->optional_string_child ("KeyId").get_value_or ("");
116
117         node->ignore_child ("Hash");
118
119         node->done ();
120 }
121
122 MainSound::MainSound (shared_ptr<const cxml::Node> node)
123 {
124         id = node->string_child ("Id");
125         annotation_text = node->optional_string_child ("AnnotationText").get_value_or ("");
126         edit_rate = Fraction (node->string_child ("EditRate"));
127         intrinsic_duration = node->number_child<int64_t> ("IntrinsicDuration");
128         entry_point = node->number_child<int64_t> ("EntryPoint");
129         duration = node->number_child<int64_t> ("Duration");
130         key_id = node->optional_string_child ("KeyId").get_value_or ("");
131         
132         node->ignore_child ("Hash");
133         node->ignore_child ("Language");
134         
135         node->done ();
136 }
137
138 MainSubtitle::MainSubtitle (shared_ptr<const cxml::Node> node)
139 {
140         id = node->string_child ("Id");
141         annotation_text = node->optional_string_child ("AnnotationText").get_value_or ("");
142         edit_rate = Fraction (node->string_child ("EditRate"));
143         intrinsic_duration = node->number_child<int64_t> ("IntrinsicDuration");
144         entry_point = node->number_child<int64_t> ("EntryPoint");
145         duration = node->number_child<int64_t> ("Duration");
146
147         node->ignore_child ("Hash");
148         node->ignore_child ("Language");
149         
150         node->done ();
151 }