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