Merge branch 'master' into speed-up
[dcpomatic.git] / src / lib / encoder.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 /** @file src/encoder.h
21  *  @brief Parent class for classes which can encode video and audio frames.
22  */
23
24 #include "encoder.h"
25 #include "util.h"
26 #include "options.h"
27
28 using std::pair;
29 using namespace boost;
30
31 int const Encoder::_history_size = 25;
32
33 /** @param f Film that we are encoding.
34  *  @param o Options.
35  */
36 Encoder::Encoder (shared_ptr<const Film> f, shared_ptr<const Options> o)
37         : _film (f)
38         , _opt (o)
39         , _just_skipped (false)
40         , _video_frame (0)
41         , _audio_frame (0)
42 {
43
44 }
45
46
47 /** @return an estimate of the current number of frames we are encoding per second,
48  *  or 0 if not known.
49  */
50 float
51 Encoder::current_frames_per_second () const
52 {
53         boost::mutex::scoped_lock lock (_history_mutex);
54         if (int (_time_history.size()) < _history_size) {
55                 return 0;
56         }
57
58         struct timeval now;
59         gettimeofday (&now, 0);
60
61         return _history_size / (seconds (now) - seconds (_time_history.back ()));
62 }
63
64 /** @return true if the last frame to be processed was skipped as it already existed */
65 bool
66 Encoder::skipping () const
67 {
68         boost::mutex::scoped_lock (_history_mutex);
69         return _just_skipped;
70 }
71
72 /** @return Number of video frames that have been received */
73 SourceFrame
74 Encoder::video_frame () const
75 {
76         boost::mutex::scoped_lock (_history_mutex);
77         return _video_frame;
78 }
79
80 /** Should be called when a frame has been encoded successfully.
81  *  @param n Source frame index.
82  */
83 void
84 Encoder::frame_done ()
85 {
86         boost::mutex::scoped_lock lock (_history_mutex);
87         _just_skipped = false;
88         
89         struct timeval tv;
90         gettimeofday (&tv, 0);
91         _time_history.push_front (tv);
92         if (int (_time_history.size()) > _history_size) {
93                 _time_history.pop_back ();
94         }
95 }
96
97 /** Called by a subclass when it has just skipped the processing
98     of a frame because it has already been done.
99 */
100 void
101 Encoder::frame_skipped ()
102 {
103         boost::mutex::scoped_lock lock (_history_mutex);
104         _just_skipped = true;
105 }
106
107 void
108 Encoder::process_video (shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
109 {
110         if (_opt->decode_video_skip != 0 && (_video_frame % _opt->decode_video_skip) != 0) {
111                 ++_video_frame;
112                 return;
113         }
114
115         if (_opt->video_decode_range) {
116                 pair<SourceFrame, SourceFrame> const r = _opt->video_decode_range.get();
117                 if (_video_frame < r.first || _video_frame >= r.second) {
118                         ++_video_frame;
119                         return;
120                 }
121         }
122
123         do_process_video (i, s);
124         ++_video_frame;
125 }
126
127 void
128 Encoder::process_audio (shared_ptr<AudioBuffers> data)
129 {
130         if (_opt->audio_decode_range) {
131
132                 shared_ptr<AudioBuffers> trimmed (new AudioBuffers (*data.get ()));
133                 
134                 /* Range that we are encoding */
135                 pair<int64_t, int64_t> required_range = _opt->audio_decode_range.get();
136                 /* Range of this block of data */
137                 pair<int64_t, int64_t> this_range (_audio_frame, _audio_frame + trimmed->frames());
138
139                 if (this_range.second < required_range.first || required_range.second < this_range.first) {
140                         /* No part of this audio is within the required range */
141                         return;
142                 } else if (required_range.first >= this_range.first && required_range.first < this_range.second) {
143                         /* Trim start */
144                         int64_t const shift = required_range.first - this_range.first;
145                         trimmed->move (shift, 0, trimmed->frames() - shift);
146                         trimmed->set_frames (trimmed->frames() - shift);
147                 } else if (required_range.second >= this_range.first && required_range.second < this_range.second) {
148                         /* Trim end */
149                         trimmed->set_frames (required_range.second - this_range.first);
150                 }
151
152                 data = trimmed;
153         }
154
155         do_process_audio (data);
156
157         _audio_frame += data->frames ();
158 }