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