Supporters update.
[dcpomatic.git] / src / lib / image_decoder.cc
1 /*
2     Copyright (C) 2012-2021 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
22 #include "exceptions.h"
23 #include "ffmpeg_image_proxy.h"
24 #include "film.h"
25 #include "frame_interval_checker.h"
26 #include "image.h"
27 #include "image_content.h"
28 #include "image_decoder.h"
29 #include "j2k_image_proxy.h"
30 #include "util.h"
31 #include "video_content.h"
32 #include "video_decoder.h"
33 #include <boost/filesystem.hpp>
34 #include <iostream>
35
36 #include "i18n.h"
37
38
39 using std::cout;
40 using std::make_shared;
41 using std::shared_ptr;
42 using dcp::Size;
43 using namespace dcpomatic;
44
45
46 ImageDecoder::ImageDecoder (shared_ptr<const Film> film, shared_ptr<const ImageContent> c)
47         : Decoder (film)
48         , _image_content (c)
49 {
50         video = make_shared<VideoDecoder>(this, c);
51 }
52
53
54 bool
55 ImageDecoder::pass ()
56 {
57         if (_frame_video_position >= _image_content->video->length()) {
58                 return true;
59         }
60
61         if (!_image_content->still() || !_image) {
62                 /* Either we need an image or we are using moving images, so load one */
63                 auto path = _image_content->path (_image_content->still() ? 0 : _frame_video_position);
64                 if (valid_j2k_file (path)) {
65                         AVPixelFormat pf;
66                         if (_image_content->video->colour_conversion()) {
67                                 /* We have a specified colour conversion: assume the image is RGB */
68                                 pf = AV_PIX_FMT_RGB48LE;
69                         } else {
70                                 /* No specified colour conversion: assume the image is XYZ */
71                                 pf = AV_PIX_FMT_XYZ12LE;
72                         }
73                         /* We can't extract image size from a JPEG2000 codestream without decoding it,
74                            so pass in the image content's size here.
75                         */
76                         auto size = _image_content->video->size();
77                         DCPOMATIC_ASSERT(size);
78                         _image = make_shared<J2KImageProxy>(path, *size, pf);
79                 } else {
80                         _image = make_shared<FFmpegImageProxy>(path);
81                 }
82         }
83
84         video->emit (film(), _image, _frame_video_position);
85         ++_frame_video_position;
86         return false;
87 }
88
89
90 void
91 ImageDecoder::seek (ContentTime time, bool accurate)
92 {
93         Decoder::seek (time, accurate);
94         _frame_video_position = time.frames_round (_image_content->active_video_frame_rate(film()));
95 }