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