Use dcp::file_to_string().
[dcpomatic.git] / src / lib / video_mxf_decoder.cc
1 /*
2     Copyright (C) 2016-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 "video_mxf_decoder.h"
23 #include "video_decoder.h"
24 #include "video_mxf_content.h"
25 #include "j2k_image_proxy.h"
26 #include "frame_interval_checker.h"
27 #include <dcp/mono_picture_asset.h>
28 #include <dcp/mono_picture_asset_reader.h>
29 #include <dcp/stereo_picture_asset.h>
30 #include <dcp/stereo_picture_asset_reader.h>
31 #include <dcp/exceptions.h>
32
33
34 using std::make_shared;
35 using std::shared_ptr;
36 using boost::optional;
37 using namespace dcpomatic;
38
39
40 VideoMXFDecoder::VideoMXFDecoder (shared_ptr<const Film> film, shared_ptr<const VideoMXFContent> content)
41         : Decoder (film)
42         , _content (content)
43 {
44         video = make_shared<VideoDecoder>(this, content);
45
46         shared_ptr<dcp::MonoPictureAsset> mono;
47         try {
48                 mono = make_shared<dcp::MonoPictureAsset>(_content->path(0));
49         } catch (dcp::MXFFileError& e) {
50                 /* maybe it's stereo */
51         } catch (dcp::ReadError& e) {
52                 /* maybe it's stereo */
53         }
54
55         shared_ptr<dcp::StereoPictureAsset> stereo;
56         try {
57                 stereo = make_shared<dcp::StereoPictureAsset>(_content->path(0));
58         } catch (dcp::MXFFileError& e) {
59                 if (!mono) {
60                         throw;
61                 }
62         } catch (dcp::ReadError& e) {
63                 if (!mono) {
64                         throw;
65                 }
66         }
67
68         if (mono) {
69                 _mono_reader = mono->start_read ();
70                 _mono_reader->set_check_hmac (false);
71                 _size = mono->size ();
72         } else {
73                 _stereo_reader = stereo->start_read ();
74                 _stereo_reader->set_check_hmac (false);
75                 _size = stereo->size ();
76         }
77 }
78
79
80 bool
81 VideoMXFDecoder::pass ()
82 {
83         auto const vfr = _content->active_video_frame_rate (film());
84         auto const frame = _next.frames_round (vfr);
85
86         if (frame >= _content->video->length()) {
87                 return true;
88         }
89
90         if (_mono_reader) {
91                 video->emit (
92                         film(),
93                         std::make_shared<J2KImageProxy>(_mono_reader->get_frame(frame), _size, AV_PIX_FMT_XYZ12LE, optional<int>()),
94                         frame
95                         );
96         } else {
97                 video->emit (
98                         film(),
99                         std::make_shared<J2KImageProxy>(_stereo_reader->get_frame(frame), _size, dcp::Eye::LEFT, AV_PIX_FMT_XYZ12LE, optional<int>()),
100                         frame
101                         );
102                 video->emit (
103                         film(),
104                         std::make_shared<J2KImageProxy>(_stereo_reader->get_frame(frame), _size, dcp::Eye::RIGHT, AV_PIX_FMT_XYZ12LE, optional<int>()),
105                         frame
106                         );
107         }
108
109         _next += ContentTime::from_frames (1, vfr);
110         return false;
111 }
112
113
114 void
115 VideoMXFDecoder::seek (ContentTime t, bool accurate)
116 {
117         Decoder::seek (t, accurate);
118         _next = t;
119 }