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