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 /** @file src/encoder.h
21 * @brief Parent class for classes which can encode video and audio frames.
29 using namespace boost;
31 int const Encoder::_history_size = 25;
33 /** @param f Film that we are encoding.
36 Encoder::Encoder (shared_ptr<const Film> f, shared_ptr<const Options> o)
39 , _just_skipped (false)
46 /** @return an estimate of the current number of frames we are encoding per second,
50 Encoder::current_frames_per_second () const
52 boost::mutex::scoped_lock lock (_history_mutex);
53 if (int (_time_history.size()) < _history_size) {
58 gettimeofday (&now, 0);
60 return _history_size / (seconds (now) - seconds (_time_history.back ()));
63 /** @return true if the last frame to be processed was skipped as it already existed */
65 Encoder::skipping () const
67 boost::mutex::scoped_lock (_history_mutex);
71 /** @return Index of last frame to be successfully encoded */
73 Encoder::last_frame () const
75 boost::mutex::scoped_lock (_history_mutex);
79 /** Should be called when a frame has been encoded successfully.
80 * @param n Source frame index.
83 Encoder::frame_done (SourceFrame n)
85 boost::mutex::scoped_lock lock (_history_mutex);
86 _just_skipped = false;
90 gettimeofday (&tv, 0);
91 _time_history.push_front (tv);
92 if (int (_time_history.size()) > _history_size) {
93 _time_history.pop_back ();
97 /** Called by a subclass when it has just skipped the processing
98 of a frame because it has already been done.
101 Encoder::frame_skipped ()
103 boost::mutex::scoped_lock lock (_history_mutex);
104 _just_skipped = true;
108 Encoder::process_video (shared_ptr<const Image> i, SourceFrame f, boost::shared_ptr<Subtitle> s)
110 if (_opt->decode_video_skip != 0 && (f % _opt->decode_video_skip) != 0) {
114 if (_opt->video_decode_range) {
115 pair<SourceFrame, SourceFrame> const r = _opt->video_decode_range.get();
116 if (f < r.first || f >= r.second) {
121 do_process_video (i, f, s);
125 Encoder::process_audio (shared_ptr<const AudioBuffers> data, int64_t f)
127 if (_opt->audio_decode_range) {
129 shared_ptr<AudioBuffers> trimmed (new AudioBuffers (*data.get ()));
131 /* Range that we are encoding */
132 pair<int64_t, int64_t> required_range = _opt->audio_decode_range.get();
133 /* Range of this block of data */
134 pair<int64_t, int64_t> this_range (f, f + trimmed->frames());
136 if (this_range.second < required_range.first || required_range.second < this_range.first) {
137 /* No part of this audio is within the required range */
139 } else if (required_range.first >= this_range.first && required_range.first < this_range.second) {
141 int64_t const shift = required_range.first - this_range.first;
142 trimmed->move (shift, 0, trimmed->frames() - shift);
143 trimmed->set_frames (trimmed->frames() - shift);
144 } else if (required_range.second >= this_range.first && required_range.second < this_range.second) {
146 trimmed->set_frames (required_range.second - this_range.first);
152 do_process_audio (data);