Add VideoMXFContent (part of #803).
[dcpomatic.git] / src / lib / video_mxf_decoder.cc
1 /*
2     Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "video_mxf_decoder.h"
21 #include "video_decoder.h"
22 #include "video_mxf_content.h"
23 #include "j2k_image_proxy.h"
24 #include <dcp/mono_picture_asset.h>
25 #include <dcp/stereo_picture_asset.h>
26 #include <dcp/exceptions.h>
27
28 using boost::shared_ptr;
29
30 VideoMXFDecoder::VideoMXFDecoder (shared_ptr<const VideoMXFContent> content, shared_ptr<Log> log)
31         : _content (content)
32 {
33         video.reset (new VideoDecoder (this, content, log));
34 }
35
36 bool
37 VideoMXFDecoder::pass (PassReason reason, bool)
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         double const vfr = _content->active_video_frame_rate ();
62         int64_t const frame = _next.frames_round (vfr);
63
64         if (mono) {
65                 video->give (shared_ptr<ImageProxy> (new J2KImageProxy (mono->get_frame(frame), mono->size())), frame);
66         } else {
67                 video->give (shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame(frame), stereo->size(), dcp::EYE_LEFT)), frame);
68                 video->give (shared_ptr<ImageProxy> (new J2KImageProxy (stereo->get_frame(frame), stereo->size(), dcp::EYE_RIGHT)), frame);
69         }
70
71         _next += ContentTime::from_frames (1, vfr);
72 }
73
74 void
75 VideoMXFDecoder::seek (ContentTime t, bool accurate)
76 {
77         video->seek (t, accurate);
78         _next = t;
79 }