12e3f3ddd46fe7356b7a336ac3a0bc95e0575eb6
[dcpomatic.git] / src / lib / decoder_factory.cc
1 /*
2     Copyright (C) 2016-2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "atmos_mxf_content.h"
22 #include "atmos_mxf_decoder.h"
23 #include "ffmpeg_content.h"
24 #include "ffmpeg_decoder.h"
25 #include "dcp_content.h"
26 #include "dcp_decoder.h"
27 #include "image_content.h"
28 #include "image_decoder.h"
29 #include "string_text_file_content.h"
30 #include "string_text_file_decoder.h"
31 #include "dcp_subtitle_content.h"
32 #include "dcp_subtitle_decoder.h"
33 #include "video_mxf_content.h"
34 #include "video_mxf_decoder.h"
35 #include "timer.h"
36 #include <boost/foreach.hpp>
37
38 using std::list;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41
42 template <class T>
43 shared_ptr<T>
44 maybe_cast (shared_ptr<Decoder> d)
45 {
46         if (!d) {
47                 return shared_ptr<T> ();
48         }
49         return dynamic_pointer_cast<T> (d);
50 }
51
52 /**
53    @param tolerant true to proceed in the face of `survivable' errors, otherwise false.
54    @param old_decoder A `used' decoder that has been previously made for this piece of content, or 0
55 */
56 shared_ptr<Decoder>
57 decoder_factory (shared_ptr<const Film> film, shared_ptr<const Content> content, bool fast, bool tolerant, shared_ptr<Decoder> old_decoder)
58 {
59         shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (content);
60         if (fc) {
61                 return shared_ptr<Decoder> (new FFmpegDecoder(film, fc, fast));
62         }
63
64         shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (content);
65         if (dc) {
66                 try {
67                         return shared_ptr<Decoder> (new DCPDecoder(film, dc, fast, tolerant, maybe_cast<DCPDecoder>(old_decoder)));
68                 } catch (KDMError& e) {
69                         /* This will be found and reported to the user when the content is examined */
70                         return shared_ptr<Decoder>();
71                 }
72         }
73
74         shared_ptr<const ImageContent> ic = dynamic_pointer_cast<const ImageContent> (content);
75         if (ic) {
76                 return shared_ptr<Decoder> (new ImageDecoder(film, ic));
77         }
78
79         shared_ptr<const StringTextFileContent> rc = dynamic_pointer_cast<const StringTextFileContent> (content);
80         if (rc) {
81                 return shared_ptr<Decoder> (new StringTextFileDecoder(film, rc));
82         }
83
84         shared_ptr<const DCPSubtitleContent> dsc = dynamic_pointer_cast<const DCPSubtitleContent> (content);
85         if (dsc) {
86                 return shared_ptr<Decoder> (new DCPSubtitleDecoder(film, dsc));
87         }
88
89         shared_ptr<const VideoMXFContent> vmc = dynamic_pointer_cast<const VideoMXFContent> (content);
90         if (vmc) {
91                 return shared_ptr<Decoder> (new VideoMXFDecoder(film, vmc));
92         }
93
94         shared_ptr<const AtmosMXFContent> amc = dynamic_pointer_cast<const AtmosMXFContent> (content);
95         if (amc) {
96                 return shared_ptr<Decoder> (new AtmosMXFDecoder(film, amc));
97         }
98
99         return shared_ptr<Decoder> ();
100 }