Port support for MXF-wrapped subtitles from 0.x
[libdcp.git] / src / mxf.cc
1 /*
2     Copyright (C) 2012-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/asset.cc
21  *  @brief Parent class for assets of DCPs made up of MXF files.
22  */
23
24 #include "AS_DCP.h"
25 #include "KM_prng.h"
26 #include "KM_util.h"
27 #include "mxf.h"
28 #include "util.h"
29 #include "metadata.h"
30 #include "exceptions.h"
31 #include "compose.hpp"
32 #include <libxml++/nodes/element.h>
33 #include <boost/filesystem.hpp>
34 #include <iostream>
35
36 using std::string;
37 using std::list;
38 using std::pair;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using namespace dcp;
42
43 MXF::MXF (Fraction edit_rate)
44         : Content (edit_rate)
45         , _encryption_context (0)
46         , _decryption_context (0)
47 {
48
49 }
50
51 MXF::MXF (boost::filesystem::path file)
52         : Content (file)
53         , _encryption_context (0)
54         , _decryption_context (0)
55 {
56
57 }
58
59 MXF::~MXF ()
60 {
61         delete _encryption_context;
62         delete _decryption_context;
63 }
64
65 void
66 MXF::fill_writer_info (ASDCP::WriterInfo* writer_info, Standard standard)
67 {
68         writer_info->ProductVersion = _metadata.product_version;
69         writer_info->CompanyName = _metadata.company_name;
70         writer_info->ProductName = _metadata.product_name.c_str();
71
72         if (standard == INTEROP) {
73                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
74         } else {
75                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
76         }
77         unsigned int c;
78         Kumu::hex2bin (_id.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
79         assert (c == Kumu::UUID_Length);
80
81         if (_key) {
82                 Kumu::GenRandomUUID (writer_info->ContextID);
83                 writer_info->EncryptedEssence = true;
84
85                 unsigned int c;
86                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
87                 assert (c == Kumu::UUID_Length);
88         }
89 }
90
91 bool
92 MXF::equals (shared_ptr<const Content> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
93 {
94         if (!Content::equals (other, opt, note)) {
95                 return false;
96         }
97         
98         shared_ptr<const MXF> other_mxf = dynamic_pointer_cast<const MXF> (other);
99         if (!other_mxf) {
100                 note (ERROR, "comparing an MXF asset with a non-MXF asset");
101                 return false;
102         }
103         
104         if (_file != other_mxf->file ()) {
105                 note (ERROR, "MXF names differ");
106                 if (!opt.mxf_names_can_differ) {
107                         return false;
108                 }
109         }
110         
111         return true;
112 }
113
114 /** Set the (private) key that will be used to encrypt or decrypt this MXF's content.
115  *  This is the top-secret key that is distributed (itself encrypted) to cinemas
116  *  via Key Delivery Messages (KDMs).
117  *  @param key Key to use.
118  */
119 void
120 MXF::set_key (Key key)
121 {
122         _key = key;
123
124         if (_key_id.empty ()) {
125                 /* No key ID so far; we now need one */
126                 _key_id = make_uuid ();
127         }
128         
129         _decryption_context = new ASDCP::AESDecContext;
130         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
131                 throw MiscError ("could not set up decryption context");
132         }
133
134         _encryption_context = new ASDCP::AESEncContext;
135         if (ASDCP_FAILURE (_encryption_context->InitKey (_key->value ()))) {
136                 throw MiscError ("could not set up encryption context");
137         }
138         
139         uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
140         
141         Kumu::FortunaRNG rng;
142         if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
143                 throw MiscError ("could not set up CBC initialization vector");
144         }
145 }
146
147 void
148 MXF::read_writer_info (ASDCP::WriterInfo const & info)
149 {
150         char buffer[64];
151         Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
152         _id = buffer;
153 }
154
155 string
156 MXF::pkl_type (Standard standard) const
157 {
158         switch (standard) {
159         case INTEROP:
160                 return String::compose ("application/x-smpte-mxf;asdcpKind=%1", asdcp_kind ());
161         case SMPTE:
162                 return "application/mxf";
163         default:
164                 assert (false);
165         }
166 }