Change MagickImageProxy to FFmpegImageProxy and make it use FFmpeg
[dcpomatic.git] / src / lib / j2k_image_proxy.cc
1 /*
2     Copyright (C) 2014-2015 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 "j2k_image_proxy.h"
22 #include "dcpomatic_socket.h"
23 #include "image.h"
24 #include "dcpomatic_assert.h"
25 #include <dcp/raw_convert.h>
26 #include <dcp/openjpeg_image.h>
27 #include <dcp/mono_picture_frame.h>
28 #include <dcp/stereo_picture_frame.h>
29 #include <dcp/colour_conversion.h>
30 #include <dcp/rgb_xyz.h>
31 #include <dcp/j2k.h>
32 #include <libcxml/cxml.h>
33 #include <libxml++/libxml++.h>
34 #include <iostream>
35
36 #include "i18n.h"
37
38 using std::string;
39 using std::cout;
40 using std::max;
41 using std::pair;
42 using std::make_pair;
43 using boost::shared_ptr;
44 using boost::optional;
45 using boost::dynamic_pointer_cast;
46 using dcp::Data;
47 using dcp::raw_convert;
48
49 /** Construct a J2KImageProxy from a JPEG2000 file */
50 J2KImageProxy::J2KImageProxy (boost::filesystem::path path, dcp::Size size, AVPixelFormat pixel_format)
51         : _data (path)
52         , _size (size)
53         , _pixel_format (pixel_format)
54 {
55         /* ::image assumes 16bpp */
56         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
57 }
58
59 J2KImageProxy::J2KImageProxy (
60         shared_ptr<const dcp::MonoPictureFrame> frame,
61         dcp::Size size,
62         AVPixelFormat pixel_format,
63         optional<int> forced_reduction
64         )
65         : _data (frame->j2k_size ())
66         , _size (size)
67         , _pixel_format (pixel_format)
68         , _forced_reduction (forced_reduction)
69 {
70         /* ::image assumes 16bpp */
71         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
72         memcpy (_data.data().get(), frame->j2k_data(), _data.size ());
73 }
74
75 J2KImageProxy::J2KImageProxy (
76         shared_ptr<const dcp::StereoPictureFrame> frame,
77         dcp::Size size,
78         dcp::Eye eye,
79         AVPixelFormat pixel_format,
80         optional<int> forced_reduction
81         )
82         : _size (size)
83         , _eye (eye)
84         , _pixel_format (pixel_format)
85         , _forced_reduction (forced_reduction)
86 {
87         /* ::image assumes 16bpp */
88         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
89         switch (eye) {
90         case dcp::EYE_LEFT:
91                 _data = Data (frame->left_j2k_size ());
92                 memcpy (_data.data().get(), frame->left_j2k_data(), _data.size ());
93                 break;
94         case dcp::EYE_RIGHT:
95                 _data = Data (frame->right_j2k_size ());
96                 memcpy (_data.data().get(), frame->right_j2k_data(), _data.size ());
97                 break;
98         }
99 }
100
101 J2KImageProxy::J2KImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
102 {
103         _size = dcp::Size (xml->number_child<int> ("Width"), xml->number_child<int> ("Height"));
104         if (xml->optional_number_child<int> ("Eye")) {
105                 _eye = static_cast<dcp::Eye> (xml->number_child<int> ("Eye"));
106         }
107         _data = Data (xml->number_child<int> ("Size"));
108         /* This only matters when we are using J2KImageProxy for the preview, which
109            will never use this constructor (which is only used for passing data to
110            encode servers).  So we can put anything in here.  It's a bit of a hack.
111         */
112         _pixel_format = AV_PIX_FMT_XYZ12LE;
113         socket->read (_data.data().get (), _data.size ());
114 }
115
116 int
117 J2KImageProxy::prepare (optional<dcp::Size> target_size) const
118 {
119         boost::mutex::scoped_lock lm (_mutex);
120
121         if (_decompressed && target_size == _target_size) {
122                 DCPOMATIC_ASSERT (_reduce);
123                 return *_reduce;
124         }
125
126         int reduce = 0;
127
128         if (_forced_reduction) {
129                 reduce = *_forced_reduction;
130         } else {
131                 while (target_size && (_size.width / pow(2, reduce)) > target_size->width && (_size.height / pow(2, reduce)) > target_size->height) {
132                         ++reduce;
133                 }
134
135                 --reduce;
136                 reduce = max (0, reduce);
137         }
138
139         _decompressed = dcp::decompress_j2k (const_cast<uint8_t*> (_data.data().get()), _data.size (), reduce);
140
141         if (_decompressed->precision(0) < 12) {
142                 int const shift = 12 - _decompressed->precision (0);
143                 for (int c = 0; c < 3; ++c) {
144                         int* p = _decompressed->data (c);
145                         for (int y = 0; y < _decompressed->size().height; ++y) {
146                                 for (int x = 0; x < _decompressed->size().width; ++x) {
147                                         *p++ <<= shift;
148                                 }
149                         }
150                 }
151         }
152
153         _target_size = target_size;
154         _reduce = reduce;
155
156         return reduce;
157 }
158
159 pair<shared_ptr<Image>, int>
160 J2KImageProxy::image (optional<dcp::NoteHandler>, optional<dcp::Size> target_size) const
161 {
162         int const reduce = prepare (target_size);
163
164         shared_ptr<Image> image (new Image (_pixel_format, _decompressed->size(), true));
165
166         /* Copy data in whatever format (sRGB or XYZ) into our Image; I'm assuming
167            the data is 12-bit either way.
168         */
169
170         int const width = _decompressed->size().width;
171
172         int p = 0;
173         for (int y = 0; y < _decompressed->size().height; ++y) {
174                 uint16_t* q = (uint16_t *) (image->data()[0] + y * image->stride()[0]);
175                 for (int x = 0; x < width; ++x) {
176                         for (int c = 0; c < 3; ++c) {
177                                 *q++ = _decompressed->data(c)[p] << 4;
178                         }
179                         ++p;
180                 }
181         }
182
183         return make_pair (image, reduce);
184 }
185
186 void
187 J2KImageProxy::add_metadata (xmlpp::Node* node) const
188 {
189         node->add_child("Type")->add_child_text (N_("J2K"));
190         node->add_child("Width")->add_child_text (raw_convert<string> (_size.width));
191         node->add_child("Height")->add_child_text (raw_convert<string> (_size.height));
192         if (_eye) {
193                 node->add_child("Eye")->add_child_text (raw_convert<string> (static_cast<int> (_eye.get ())));
194         }
195         node->add_child("Size")->add_child_text (raw_convert<string> (_data.size ()));
196 }
197
198 void
199 J2KImageProxy::send_binary (shared_ptr<Socket> socket) const
200 {
201         socket->write (_data.data().get(), _data.size());
202 }
203
204 bool
205 J2KImageProxy::same (shared_ptr<const ImageProxy> other) const
206 {
207         shared_ptr<const J2KImageProxy> jp = dynamic_pointer_cast<const J2KImageProxy> (other);
208         if (!jp) {
209                 return false;
210         }
211
212         if (_data.size() != jp->_data.size()) {
213                 return false;
214         }
215
216         return memcmp (_data.data().get(), jp->_data.data().get(), _data.size()) == 0;
217 }
218
219 J2KImageProxy::J2KImageProxy (Data data, dcp::Size size, AVPixelFormat pixel_format)
220         : _data (data)
221         , _size (size)
222         , _pixel_format (pixel_format)
223 {
224         /* ::image assumes 16bpp */
225         DCPOMATIC_ASSERT (_pixel_format == AV_PIX_FMT_RGB48 || _pixel_format == AV_PIX_FMT_XYZ12LE);
226 }
227
228 size_t
229 J2KImageProxy::memory_used () const
230 {
231         size_t m = _data.size();
232         if (_decompressed) {
233                 /* 3 components, 16-bits per pixel */
234                 m += 3 * 2 * _decompressed->size().width * _decompressed->size().height;
235         }
236         return m;
237 }