Merge branch '1.0' of ssh://houllier/home/carl/git/dvdomatic into 1.0
[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->crop (_video_content->crop(), true);
55
56         shared_ptr<const Film> film = _film.lock ();
57         assert (film);
58         
59         libdcp::Size const container_size = 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         shared_ptr<Subtitle> sub;
65         if (_timed_subtitle && _timed_subtitle->displayed_at (t)) {
66                 sub = _timed_subtitle->subtitle ();
67         }
68
69         if (sub) {
70                 dcpomatic::Rect const tx = subtitle_transformed_area (
71                         float (image_size.width) / video_size().width,
72                         float (image_size.height) / video_size().height,
73                         sub->area(), film->subtitle_offset(), film->subtitle_scale()
74                         );
75
76                 shared_ptr<Image> im = sub->image()->scale (tx.size(), film->scaler(), true);
77                 out->alpha_blend (im, tx.position());
78         }
79
80         if (image_size != container_size) {
81                 assert (image_size.width <= container_size.width);
82                 assert (image_size.height <= container_size.height);
83                 shared_ptr<Image> im (new SimpleImage (PIX_FMT_RGB24, container_size, true));
84                 im->make_black ();
85                 im->copy (out, Position ((container_size.width - image_size.width) / 2, (container_size.height - image_size.height) / 2));
86                 out = im;
87         }
88                 
89         Video (out, same, t);
90
91         if (_frame_rate_conversion.repeat) {
92                 Video (image, true, t + film->video_frames_to_time (1));
93                 _next_video = t + film->video_frames_to_time (2);
94         } else {
95                 _next_video = t + film->video_frames_to_time (1);
96         }
97
98         _odd = !_odd;
99 }
100
101 /** Called by subclasses when a subtitle is ready.
102  *  s may be 0 to say that there is no current subtitle.
103  *  @param s New current subtitle, or 0.
104  */
105 void
106 VideoDecoder::subtitle (shared_ptr<TimedSubtitle> s)
107 {
108         _timed_subtitle = s;
109         
110         if (_timed_subtitle) {
111                 Position const p = _timed_subtitle->subtitle()->position ();
112                 _timed_subtitle->subtitle()->set_position (Position (p.x - _video_content->crop().left, p.y - _video_content->crop().top));
113         }
114 }
115
116 bool
117 VideoDecoder::video_done () const
118 {
119         shared_ptr<const Film> film = _film.lock ();
120         assert (film);
121         
122         return (_video_content->length() - _next_video) < film->video_frames_to_time (1);
123 }
124
125 void
126 VideoDecoder::seek (Time t)
127 {
128         _next_video = t;
129 }
130
131 void
132 VideoDecoder::seek_back ()
133 {
134         shared_ptr<const Film> film = _film.lock ();
135         assert (film);
136         _next_video -= film->video_frames_to_time (1);
137 }
138
139 void
140 VideoDecoder::seek_forward ()
141 {
142         shared_ptr<const Film> film = _film.lock ();
143         assert (film);
144         _next_video += film->video_frames_to_time (1);
145 }
146
147