Give Film a container; move crop into video content; other bits.
[dcpomatic.git] / src / lib / video_decoder.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include "video_decoder.h"
23 #include "subtitle.h"
24 #include "film.h"
25 #include "image.h"
26 #include "log.h"
27 #include "job.h"
28
29 #include "i18n.h"
30
31 using std::cout;
32 using boost::shared_ptr;
33 using boost::optional;
34
35 VideoDecoder::VideoDecoder (shared_ptr<const Film> f, shared_ptr<const VideoContent> c)
36         : Decoder (f)
37         , _video_content (c)
38         , _video_frame (0)
39         , _last_content_time (0)
40 {
41
42 }
43
44 /** Called by subclasses to tell the world that some video data is ready.
45  *  We find a subtitle then emit it for listeners.
46  *  @param image frame to emit.
47  *  @param t Time of the frame within the source.
48  */
49 void
50 VideoDecoder::emit_video (shared_ptr<Image> image, bool same, Time t)
51 {
52         shared_ptr<Subtitle> sub;
53         if (_timed_subtitle && _timed_subtitle->displayed_at (t)) {
54                 sub = _timed_subtitle->subtitle ();
55         }
56
57         TIMING (N_("Decoder emits %1"), _video_frame);
58         Video (image, same, sub, t);
59         ++_video_frame;
60
61         _last_content_time = t;
62 }
63
64 /** Set up the current subtitle.  This will be put onto frames that
65  *  fit within its time specification.  s may be 0 to say that there
66  *  is no current subtitle.
67  *  @param s New current subtitle, or 0.
68  */
69 void
70 VideoDecoder::emit_subtitle (shared_ptr<TimedSubtitle> s)
71 {
72         _timed_subtitle = s;
73         
74         if (_timed_subtitle) {
75                 Position const p = _timed_subtitle->subtitle()->position ();
76                 _timed_subtitle->subtitle()->set_position (Position (p.x - _video_content->crop().left, p.y - _video_content->crop().top));
77         }
78 }
79
80 void
81 VideoDecoder::set_progress (Job* j) const
82 {
83         assert (j);
84
85         if (_film->length()) {
86                 j->set_progress (float (_video_frame) / _film->time_to_video_frames (_film->length()));
87         }
88 }