2c050def67e8098e63056640fac2d095b7ad650d
[dcpomatic.git] / src / lib / tiff_decoder.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 /** @file src/tiff_decoder.cc
21  *  @brief A decoder which reads a numbered set of TIFF files, one per frame.
22  */
23
24 #include <vector>
25 #include <string>
26 #include <iostream>
27 #include <stdint.h>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/filesystem.hpp>
30 #include <tiffio.h>
31 extern "C" {
32 #include <libavformat/avformat.h>
33 }
34 #include "util.h"
35 #include "tiff_decoder.h"
36 #include "exceptions.h"
37 #include "image.h"
38 #include "options.h"
39 #include "film.h"
40
41 using namespace std;
42 using namespace boost;
43
44 /** @param f Our Film.
45  *  @param o Options.
46  *  @param j Job that we are associated with, or 0.
47  *  @param minimal true to do the bare minimum of work; just run through the content.  Useful for acquiring
48  *  accurate frame counts as quickly as possible.  This generates no video or audio output.
49  */
50 TIFFDecoder::TIFFDecoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal)
51         : Decoder (f, o, j, minimal)
52 {
53         string const dir = _film->content_path ();
54         
55         if (!filesystem::is_directory (dir)) {
56                 throw DecodeError ("TIFF content must be in a directory");
57         }
58
59         for (filesystem::directory_iterator i = filesystem::directory_iterator (dir); i != filesystem::directory_iterator(); ++i) {
60                 /* Aah, the sweet smell of progress */
61 #if BOOST_FILESYSTEM_VERSION == 3
62                 string const ext = filesystem::path(*i).extension().string();
63                 string const l = filesystem::path(*i).leaf().generic_string();
64 #else
65                 string const ext = filesystem::path(*i).extension();
66                 string const l = i->leaf ();
67 #endif
68                 if (ext == ".tif" || ext == ".tiff") {
69                         _files.push_back (l);
70                 }
71         }
72
73         _files.sort ();
74
75         _iter = _files.begin ();
76
77 }
78
79 float
80 TIFFDecoder::frames_per_second () const
81 {
82         /* We don't know */
83         return 0;
84 }
85
86 Size
87 TIFFDecoder::native_size () const
88 {
89         if (_files.empty ()) {
90                 throw DecodeError ("no TIFF files found");
91         }
92         
93         TIFF* t = TIFFOpen (file_path (_files.front()).c_str (), "r");
94         if (t == 0) {
95                 throw DecodeError ("could not open TIFF file");
96         }
97
98         uint32_t width;
99         TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
100         uint32_t height;
101         TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);
102
103         return Size (width, height);
104 }
105
106 int
107 TIFFDecoder::audio_channels () const
108 {
109         /* No audio */
110         return 0;
111 }
112
113 int
114 TIFFDecoder::audio_sample_rate () const
115 {
116         return 0;
117 }
118
119 AVSampleFormat
120 TIFFDecoder::audio_sample_format () const
121 {
122         return AV_SAMPLE_FMT_NONE;
123 }
124
125
126 int64_t
127 TIFFDecoder::audio_channel_layout () const
128 {
129         return 0;
130 }
131
132 bool
133 TIFFDecoder::pass ()
134 {
135         if (_iter == _files.end ()) {
136                 return true;
137         }
138
139         TIFF* t = TIFFOpen (file_path (*_iter).c_str (), "r");
140         if (t == 0) {
141                 throw DecodeError ("could not open TIFF file");
142         }
143
144         uint32_t width;
145         TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
146         uint32_t height;
147         TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);
148
149         int const num_pixels = width * height;
150         uint32_t * raster = (uint32_t *) _TIFFmalloc (num_pixels * sizeof (uint32_t));
151         if (raster == 0) {
152                 throw DecodeError ("could not allocate memory to decode TIFF file");
153         }
154
155         if (TIFFReadRGBAImage (t, width, height, raster, 0) < 0) {
156                 throw DecodeError ("could not read TIFF data");
157         }
158
159         RGBFrameImage image (Size (width, height));
160
161         uint8_t* p = image.data()[0];
162         for (uint32_t y = 0; y < height; ++y) {
163                 for (uint32_t x = 0; x < width; ++x) {
164                         uint32_t const i = (height - y - 1) * width + x;
165                         *p++ = raster[i] & 0xff;
166                         *p++ = (raster[i] & 0xff00) >> 8;
167                         *p++ = (raster[i] & 0xff0000) >> 16;
168                 }
169         }
170
171         _TIFFfree (raster);
172         TIFFClose (t);
173
174         process_video (image.frame ());
175
176         ++_iter;
177         return false;
178 }
179
180 /** @param file name within our content directory
181  *  @return full path to the file
182  */
183 string
184 TIFFDecoder::file_path (string f) const
185 {
186         stringstream s;
187         s << _film->content_path() << "/" << f;
188         return _film->file (s.str ());
189 }
190
191 PixelFormat
192 TIFFDecoder::pixel_format () const
193 {
194         return PIX_FMT_RGB24;
195 }
196
197 int
198 TIFFDecoder::time_base_numerator () const
199 {
200         return dcp_frame_rate(_film->frames_per_second()).frames_per_second;
201 }
202
203
204 int
205 TIFFDecoder::time_base_denominator () const
206 {
207         return 1;
208 }
209
210 int
211 TIFFDecoder::sample_aspect_ratio_numerator () const
212 {
213         /* XXX */
214         return 1;
215 }
216
217 int
218 TIFFDecoder::sample_aspect_ratio_denominator () const
219 {
220         /* XXX */
221         return 1;
222 }