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