7d8559a7c5656c7add550abc1f9daf5625f5126e
[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  *  @param ignore_length Ignore the content's claimed length when computing progress.
50  */
51 TIFFDecoder::TIFFDecoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
52         : Decoder (f, o, j, minimal, ignore_length)
53 {
54         string const dir = _film->content_path ();
55         
56         if (!filesystem::is_directory (dir)) {
57                 throw DecodeError ("TIFF content must be in a directory");
58         }
59
60         for (filesystem::directory_iterator i = filesystem::directory_iterator (dir); i != filesystem::directory_iterator(); ++i) {
61                 /* Aah, the sweet smell of progress */
62 #if BOOST_FILESYSTEM_VERSION == 3
63                 string const ext = filesystem::path(*i).extension().string();
64                 string const l = filesystem::path(*i).leaf().generic_string();
65 #else
66                 string const ext = filesystem::path(*i).extension();
67                 string const l = i->leaf ();
68 #endif
69                 if (ext == ".tif" || ext == ".tiff") {
70                         _files.push_back (l);
71                 }
72         }
73
74         _files.sort ();
75
76         _iter = _files.begin ();
77
78 }
79
80 float
81 TIFFDecoder::frames_per_second () const
82 {
83         /* We don't know */
84         return 0;
85 }
86
87 Size
88 TIFFDecoder::native_size () const
89 {
90         if (_files.empty ()) {
91                 throw DecodeError ("no TIFF files found");
92         }
93         
94         TIFF* t = TIFFOpen (file_path (_files.front()).c_str (), "r");
95         if (t == 0) {
96                 throw DecodeError ("could not open TIFF file");
97         }
98
99         uint32_t width;
100         TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
101         uint32_t height;
102         TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);
103
104         return Size (width, height);
105 }
106
107 int
108 TIFFDecoder::audio_channels () const
109 {
110         /* No audio */
111         return 0;
112 }
113
114 int
115 TIFFDecoder::audio_sample_rate () const
116 {
117         return 0;
118 }
119
120 AVSampleFormat
121 TIFFDecoder::audio_sample_format () const
122 {
123         return AV_SAMPLE_FMT_NONE;
124 }
125
126
127 int64_t
128 TIFFDecoder::audio_channel_layout () const
129 {
130         return 0;
131 }
132
133 bool
134 TIFFDecoder::pass ()
135 {
136         if (_iter == _files.end ()) {
137                 return true;
138         }
139
140         TIFF* t = TIFFOpen (file_path (*_iter).c_str (), "r");
141         if (t == 0) {
142                 throw DecodeError ("could not open TIFF file");
143         }
144
145         uint32_t width;
146         TIFFGetField (t, TIFFTAG_IMAGEWIDTH, &width);
147         uint32_t height;
148         TIFFGetField (t, TIFFTAG_IMAGELENGTH, &height);
149
150         int const num_pixels = width * height;
151         uint32_t * raster = (uint32_t *) _TIFFmalloc (num_pixels * sizeof (uint32_t));
152         if (raster == 0) {
153                 throw DecodeError ("could not allocate memory to decode TIFF file");
154         }
155
156         if (TIFFReadRGBAImage (t, width, height, raster, 0) < 0) {
157                 throw DecodeError ("could not read TIFF data");
158         }
159
160         RGBFrameImage image (Size (width, height));
161
162         uint8_t* p = image.data()[0];
163         for (uint32_t y = 0; y < height; ++y) {
164                 for (uint32_t x = 0; x < width; ++x) {
165                         uint32_t const i = (height - y - 1) * width + x;
166                         *p++ = raster[i] & 0xff;
167                         *p++ = (raster[i] & 0xff00) >> 8;
168                         *p++ = (raster[i] & 0xff0000) >> 16;
169                 }
170         }
171
172         _TIFFfree (raster);
173         TIFFClose (t);
174
175         process_video (image.frame ());
176
177         ++_iter;
178         return false;
179 }
180
181 /** @param file name within our content directory
182  *  @return full path to the file
183  */
184 string
185 TIFFDecoder::file_path (string f) const
186 {
187         stringstream s;
188         s << _film->content_path() << "/" << f;
189         return _film->file (s.str ());
190 }
191
192 PixelFormat
193 TIFFDecoder::pixel_format () const
194 {
195         return PIX_FMT_RGB24;
196 }
197
198 int
199 TIFFDecoder::time_base_numerator () const
200 {
201         return dcp_frame_rate(_film->frames_per_second()).frames_per_second;
202 }
203
204
205 int
206 TIFFDecoder::time_base_denominator () const
207 {
208         return 1;
209 }
210
211 int
212 TIFFDecoder::sample_aspect_ratio_numerator () const
213 {
214         /* XXX */
215         return 1;
216 }
217
218 int
219 TIFFDecoder::sample_aspect_ratio_denominator () const
220 {
221         /* XXX */
222         return 1;
223 }