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