Allow changing of MXF directory / filename; un-expose ASDCP frame buffer classes...
[libdcp.git] / src / picture_frame.cc
1 /*
2     Copyright (C) 2012 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 <openjpeg.h>
21 #include "AS_DCP.h"
22 #include "KM_fileio.h"
23 #include "picture_frame.h"
24 #include "exceptions.h"
25 #include "argb_frame.h"
26 #include "lut.h"
27 #include "util.h"
28
29 using std::string;
30 using boost::shared_ptr;
31 using namespace libdcp;
32
33 /** Make a picture frame from a 2D (monoscopic) asset.
34  *  @param mxf_path Path to the asset's MXF file.
35  *  @param n Frame within the asset, not taking EntryPoint into account.
36  */
37 MonoPictureFrame::MonoPictureFrame (string mxf_path, int n)
38 {
39         ASDCP::JP2K::MXFReader reader;
40         if (ASDCP_FAILURE (reader.OpenRead (mxf_path.c_str()))) {
41                 throw FileError ("could not open MXF file for reading", mxf_path);
42         }
43
44         /* XXX: unfortunate guesswork on this buffer size */
45         _buffer = new ASDCP::JP2K::FrameBuffer (4 * Kumu::Megabyte);
46
47         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer))) {
48                 throw DCPReadError ("could not read video frame");
49         }
50 }
51
52 MonoPictureFrame::~MonoPictureFrame ()
53 {
54         delete _buffer;
55 }
56
57 uint8_t const *
58 MonoPictureFrame::j2k_data () const
59 {
60         return _buffer->RoData ();
61 }
62
63 int
64 MonoPictureFrame::j2k_size () const
65 {
66         return _buffer->Size ();
67 }
68
69 /** @param reduce a factor by which to reduce the resolution
70  *  of the image, expressed as a power of two (pass 0 for no
71  *  reduction).
72  *
73  *  @return An ARGB representation of this frame.  This is ARGB in the
74  *  Cairo sense, so that each pixel takes up 4 bytes; the first byte
75  *  is blue, second green, third red and fourth alpha (always 255).
76  *
77  */
78 shared_ptr<ARGBFrame>
79 MonoPictureFrame::argb_frame (int reduce) const
80 {
81         opj_image_t* xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->RoData()), _buffer->Size(), reduce);
82         assert (xyz_frame->numcomps == 3);
83         shared_ptr<ARGBFrame> f = xyz_to_rgb (xyz_frame);
84         opj_image_destroy (xyz_frame);
85         return f;
86 }
87
88 /** Make a picture frame from a 3D (stereoscopic) asset.
89  *  @param mxf_path Path to the asset's MXF file.
90  *  @param n Frame within the asset, not taking EntryPoint into account.
91  */
92 StereoPictureFrame::StereoPictureFrame (string mxf_path, int n)
93 {
94         ASDCP::JP2K::MXFSReader reader;
95         if (ASDCP_FAILURE (reader.OpenRead (mxf_path.c_str()))) {
96                 throw FileError ("could not open MXF file for reading", mxf_path);
97         }
98
99         /* XXX: unfortunate guesswork on this buffer size */
100         _buffer = new ASDCP::JP2K::SFrameBuffer (4 * Kumu::Megabyte);
101
102         if (ASDCP_FAILURE (reader.ReadFrame (n, *_buffer))) {
103                 throw DCPReadError ("could not read video frame");
104         }
105 }
106
107 StereoPictureFrame::~StereoPictureFrame ()
108 {
109         delete _buffer;
110 }
111
112 /** @param reduce a factor by which to reduce the resolution
113  *  of the image, expressed as a power of two (pass 0 for no
114  *  reduction).
115  *
116  *  @param eye Eye to return (EYE_LEFT or EYE_RIGHT).
117  *
118  *  @return An ARGB representation of one of the eyes (left or right)
119  *  of this frame.  This is ARGB in the Cairo sense, so that each
120  *  pixel takes up 4 bytes; the first byte is blue, second green,
121  *  third red and fourth alpha (always 255).
122  *
123  */
124 shared_ptr<ARGBFrame>
125 StereoPictureFrame::argb_frame (Eye eye, int reduce) const
126 {
127         opj_image_t* xyz_frame = 0;
128         switch (eye) {
129         case LEFT:
130                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Left.RoData()), _buffer->Left.Size(), reduce);
131                 break;
132         case RIGHT:
133                 xyz_frame = decompress_j2k (const_cast<uint8_t*> (_buffer->Right.RoData()), _buffer->Right.Size(), reduce);
134                 break;
135         }
136         
137         assert (xyz_frame->numcomps == 3);
138         shared_ptr<ARGBFrame> f = xyz_to_rgb (xyz_frame);
139         opj_image_destroy (xyz_frame);
140         return f;
141 }
142
143 uint8_t const *
144 StereoPictureFrame::left_j2k_data () const
145 {
146         return _buffer->Left.RoData ();
147 }
148
149 int
150 StereoPictureFrame::left_j2k_size () const
151 {
152         return _buffer->Left.Size ();
153 }
154
155 uint8_t const *
156 StereoPictureFrame::right_j2k_data () const
157 {
158         return _buffer->Right.RoData ();
159 }
160
161 int
162 StereoPictureFrame::right_j2k_size () const
163 {
164         return _buffer->Right.Size ();
165 }