2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "video_decoder.h"
29 using boost::shared_ptr;
31 VideoDecoder::VideoDecoder (shared_ptr<const Film> f, shared_ptr<const VideoContent> c)
35 , _frame_rate_conversion (c->video_frame_rate(), f->dcp_video_frame_rate())
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.
47 VideoDecoder::video (shared_ptr<Image> image, bool same, Time t)
49 if (_frame_rate_conversion.skip && _odd) {
54 image = image->crop (_video_content->crop(), true);
56 shared_ptr<const Film> film = _film.lock ();
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);
62 shared_ptr<Image> out = image->scale_and_convert_to_rgb (image_size, film->scaler(), true);
64 if (film->with_subtitles ()) {
65 shared_ptr<Subtitle> sub;
66 if (_timed_subtitle && _timed_subtitle->displayed_at (t)) {
67 sub = _timed_subtitle->subtitle ();
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()
77 shared_ptr<Image> im = sub->image()->scale (tx.size(), film->scaler(), true);
78 out->alpha_blend (im, tx.position());
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));
87 im->copy (out, Position ((container_size.width - image_size.width) / 2, (container_size.height - image_size.height) / 2));
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);
97 _next_video = t + film->video_frames_to_time (1);
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.
108 VideoDecoder::subtitle (shared_ptr<TimedSubtitle> s)
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));
119 VideoDecoder::video_done () const
121 shared_ptr<const Film> film = _film.lock ();
124 return (_video_content->length() - _next_video) < film->video_frames_to_time (1);
128 VideoDecoder::seek (Time t)
134 VideoDecoder::seek_back ()
136 shared_ptr<const Film> film = _film.lock ();
138 _next_video -= film->video_frames_to_time (1);
142 VideoDecoder::seek_forward ()
144 shared_ptr<const Film> film = _film.lock ();
146 _next_video += film->video_frames_to_time (1);
150 VideoDecoder::set_video_container_size (libdcp::Size s)
152 _video_container_size = s;