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->crop (_video_content->crop(), true);
56 shared_ptr<const Film> film = _film.lock ();
59 libdcp::Size const container_size = 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 shared_ptr<Subtitle> sub;
65 if (_timed_subtitle && _timed_subtitle->displayed_at (t)) {
66 sub = _timed_subtitle->subtitle ();
70 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()
76 shared_ptr<Image> im = sub->image()->scale (tx.size(), film->scaler(), true);
77 out->alpha_blend (im, tx.position());
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));
85 im->copy (out, Position ((container_size.width - image_size.width) / 2, (container_size.height - image_size.height) / 2));
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);
95 _next_video = t + film->video_frames_to_time (1);
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.
106 VideoDecoder::subtitle (shared_ptr<TimedSubtitle> s)
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));
117 VideoDecoder::video_done () const
119 shared_ptr<const Film> film = _film.lock ();
122 return (_video_content->length() - _next_video) < film->video_frames_to_time (1);