Untested external audio support; AB transcodes still broken.
[dcpomatic.git] / src / lib / video_decoder.cc
1 #include "video_decoder.h"
2 #include "subtitle.h"
3 #include "film.h"
4 #include "image.h"
5 #include "log.h"
6 #include "options.h"
7 #include "job.h"
8
9 using boost::shared_ptr;
10 using boost::optional;
11
12 VideoDecoder::VideoDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, Job* j)
13         : Decoder (f, o, j)
14         , _video_frame (0)
15 {
16
17 }
18
19 /** Called by subclasses to tell the world that some video data is ready.
20  *  We find a subtitle then emit it for listeners.
21  *  @param frame to decode; caller manages memory.
22  */
23 void
24 VideoDecoder::emit_video (shared_ptr<Image> image)
25 {
26         shared_ptr<Subtitle> sub;
27         if (_timed_subtitle && _timed_subtitle->displayed_at (double (video_frame()) / _film->frames_per_second())) {
28                 sub = _timed_subtitle->subtitle ();
29         }
30
31         signal_video (image, sub);
32 }
33
34 void
35 VideoDecoder::repeat_last_video ()
36 {
37         if (!_last_image) {
38                 _last_image.reset (new CompactImage (pixel_format(), native_size()));
39                 _last_image->make_black ();
40         }
41
42         signal_video (_last_image, _last_subtitle);
43 }
44
45 void
46 VideoDecoder::signal_video (shared_ptr<Image> image, shared_ptr<Subtitle> sub)
47 {
48         TIMING ("Decoder emits %1", _video_frame);
49         Video (image, sub);
50         ++_video_frame;
51
52         _last_image = image;
53         _last_subtitle = sub;
54 }
55
56 void
57 VideoDecoder::emit_subtitle (shared_ptr<TimedSubtitle> s)
58 {
59         _timed_subtitle = s;
60         
61         if (_timed_subtitle && _opt->apply_crop) {
62                 Position const p = _timed_subtitle->subtitle()->position ();
63                 _timed_subtitle->subtitle()->set_position (Position (p.x - _film->crop().left, p.y - _film->crop().top));
64         }
65 }
66
67 void
68 VideoDecoder::set_subtitle_stream (optional<SubtitleStream> s)
69 {
70         _subtitle_stream = s;
71 }
72
73 void
74 VideoDecoder::set_progress () const
75 {
76         if (_job && _film->dcp_length()) {
77                 _job->set_progress (float (_video_frame) / _film->length().get());
78         }
79 }