fe6959cd535e8e7068906fa4b4a97a77998f3112
[dcpomatic.git] / src / lib / jpeg2000_encoder.cc
1 /*
2     Copyright (C) 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 "jpeg2000_encoder.h"
21 #include "poznan_encoder.h"
22 #include "openjpeg_encoder.h"
23 #include "exceptions.h"
24 #include <boost/shared_ptr.hpp>
25 #include <boost/foreach.hpp>
26 #include <vector>
27
28 using std::vector;
29 using std::string;
30 using boost::shared_ptr;
31
32 #define LOG_GENERAL(...) log()->log (String::compose (__VA_ARGS__), Log::TYPE_GENERAL);
33
34 vector<boost::shared_ptr<JPEG2000Encoder> > JPEG2000Encoder::_encoders;
35
36 void
37 JPEG2000Encoder::setup_encoders ()
38 {
39         try {
40                 _encoders.push_back (shared_ptr<JPEG2000Encoder> (new PoznanEncoder ()));
41         } catch (JPEG2000EncoderUnavailableException& e) {
42                 std::cerr << e.what() << "\n";
43         }
44
45         try {
46                 _encoders.push_back (shared_ptr<JPEG2000Encoder> (new OpenJPEGEncoder ()));
47         } catch (JPEG2000EncoderUnavailableException &) {
48
49         }
50 }
51
52 vector<shared_ptr<JPEG2000Encoder> >
53 JPEG2000Encoder::all ()
54 {
55         return _encoders;
56 }
57
58 shared_ptr<JPEG2000Encoder>
59 JPEG2000Encoder::from_id (string id)
60 {
61         BOOST_FOREACH (shared_ptr<JPEG2000Encoder> i, _encoders) {
62                 if (i->id() == id) {
63                         return i;
64                 }
65         }
66
67         return shared_ptr<JPEG2000Encoder> ();
68 }
69
70 Data
71 JPEG2000Encoder::encode (shared_ptr<const dcp::OpenJPEGImage> input, int bandwidth, int frame_rate, Resolution resolution, bool threed)
72 {
73         std::cout << "Encoding with " << name() << "\n";
74
75         if (!_bandwidth || _bandwidth.get() != bandwidth ||
76             !_frame_rate || _frame_rate.get() != frame_rate ||
77             !_resolution || _resolution.get() != resolution ||
78             !_threed || _threed.get() != threed) {
79
80                 _bandwidth = bandwidth;
81                 _frame_rate = frame_rate;
82                 _resolution = resolution;
83                 _threed = threed;
84
85                 parameters_changed ();
86         }
87
88         return do_encode (input);
89 }