Fix subtitle controls in the viewer.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /*
2     Copyright (C) 2012 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_decoder.h"
21 #include "subtitle.h"
22 #include "film.h"
23 #include "image.h"
24 #include "ratio.h"
25
26 #include "i18n.h"
27
28 using std::cout;
29 using boost::shared_ptr;
30
31 VideoDecoder::VideoDecoder (shared_ptr<const Film> f, shared_ptr<const VideoContent> c)
32         : Decoder (f)
33         , _next_video (0)
34         , _video_content (c)
35         , _frame_rate_conversion (c->video_frame_rate(), f->dcp_video_frame_rate())
36         , _odd (false)
37 {
38
39 }
40
41 /** Called by subclasses when some video is ready.
42  *  @param image frame to emit.
43  *  @param same true if this frame is the same as the last one passed to this call.
44  *  @param t Time of the frame within the source.
45  */
46 void
47 VideoDecoder::video (shared_ptr<Image> image, bool same, Time t)
48 {
49         if (_frame_rate_conversion.skip && _odd) {
50                 _odd = !_odd;
51                 return;
52         }
53
54         image = image->crop (_video_content->crop(), true);
55
56         shared_ptr<const Film> film = _film.lock ();
57         assert (film);
58         
59         libdcp::Size const container_size = _video_container_size.get_value_or (film->container()->size (film->full_frame ()));
60         libdcp::Size const image_size = _video_content->ratio()->size (container_size);
61         
62         shared_ptr<Image> out = image->scale_and_convert_to_rgb (image_size, film->scaler(), true);
63
64         if (film->with_subtitles ()) {
65                 shared_ptr<Subtitle> sub;
66                 if (_timed_subtitle && _timed_subtitle->displayed_at (t)) {
67                         sub = _timed_subtitle->subtitle ();
68                 }
69                 
70                 if (sub) {
71                         dcpomatic::Rect const tx = subtitle_transformed_area (
72                                 float (image_size.width) / video_size().width,
73                                 float (image_size.height) / video_size().height,
74                                 sub->area(), film->subtitle_offset(), film->subtitle_scale()
75                                 );
76                         
77                         shared_ptr<Image> im = sub->image()->scale (tx.size(), film->scaler(), true);
78                         out->alpha_blend (im, tx.position());
79                 }
80         }
81
82         if (image_size != container_size) {
83                 assert (image_size.width <= container_size.width);
84                 assert (image_size.height <= container_size.height);
85                 shared_ptr<Image> im (new SimpleImage (PIX_FMT_RGB24, container_size, true));
86                 im->make_black ();
87                 im->copy (out, Position ((container_size.width - image_size.width) / 2, (container_size.height - image_size.height) / 2));
88                 out = im;
89         }
90                 
91         Video (out, same, t);
92
93         if (_frame_rate_conversion.repeat) {
94                 Video (image, true, t + film->video_frames_to_time (1));
95                 _next_video = t + film->video_frames_to_time (2);
96         } else {
97                 _next_video = t + film->video_frames_to_time (1);
98         }
99
100         _odd = !_odd;
101 }
102
103 /** Called by subclasses when a subtitle is ready.
104  *  s may be 0 to say that there is no current subtitle.
105  *  @param s New current subtitle, or 0.
106  */
107 void
108 VideoDecoder::subtitle (shared_ptr<TimedSubtitle> s)
109 {
110         _timed_subtitle = s;
111         
112         if (_timed_subtitle) {
113                 Position const p = _timed_subtitle->subtitle()->position ();
114                 _timed_subtitle->subtitle()->set_position (Position (p.x - _video_content->crop().left, p.y - _video_content->crop().top));
115         }
116 }
117
118 bool
119 VideoDecoder::video_done () const
120 {
121         shared_ptr<const Film> film = _film.lock ();
122         assert (film);
123         
124         return (_video_content->length() - _next_video) < film->video_frames_to_time (1);
125 }
126
127 void
128 VideoDecoder::seek (Time t)
129 {
130         _next_video = t;
131 }
132
133 void
134 VideoDecoder::seek_back ()
135 {
136         shared_ptr<const Film> film = _film.lock ();
137         assert (film);
138         _next_video -= film->video_frames_to_time (1);
139 }
140
141 void
142 VideoDecoder::seek_forward ()
143 {
144         shared_ptr<const Film> film = _film.lock ();
145         assert (film);
146         _next_video += film->video_frames_to_time (1);
147 }
148
149 void
150 VideoDecoder::set_video_container_size (libdcp::Size s)
151 {
152         _video_container_size = s;
153 }