Simplify time representation; better in-tree DCP subtitle parser.
[libsub.git] / src / smpte_dcp_reader.cc
1 /*
2     Copyright (C) 2014-2015 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 #include "smpte_dcp_reader.h"
21 #include "exceptions.h"
22 #include "xml.h"
23 #include "AS_DCP.h"
24 #include "KM_util.h"
25 #include "dcp/font.h"
26 #include "dcp/smpte_load_font.h"
27 #include <libcxml/cxml.h>
28 #include <boost/foreach.hpp>
29
30 using std::string;
31 using std::list;
32 using std::stringstream;
33 using boost::shared_ptr;
34 using namespace sub;
35
36 SMPTEDCPReader::SMPTEDCPReader (boost::filesystem::path file, bool mxf)
37 {
38         shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
39         
40         if (mxf) {
41                 ASDCP::TimedText::MXFReader reader;
42                 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
43                 if (ASDCP_FAILURE (r)) {
44                         boost::throw_exception (MXFError ("could not open MXF file for reading"));
45                 }
46         
47                 string s;
48                 reader.ReadTimedTextResource (s, 0, 0);
49                 stringstream t;
50                 t << s;
51                 xml->read_stream (t);
52
53                 ASDCP::WriterInfo info;
54                 reader.FillWriterInfo (info);
55                 
56                 char buffer[64];
57                 Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
58                 _id = buffer;
59         } else {
60                 xml->read_file (file);
61                 _id = xml->string_child("Id").substr (9);
62         }
63         
64         _load_font_nodes = type_children<dcp::SMPTELoadFont> (xml, "LoadFont");
65
66         parse_common (xml, xml->number_child<int> ("TimeCodeRate"));
67 }