Merge FilmState / Film.
[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
27 using namespace boost;
28
29 int const Encoder::_history_size = 25;
30
31 /** @param f Film that we are encoding.
32  *  @param o Options.
33  */
34 Encoder::Encoder (shared_ptr<const Film> f, shared_ptr<const Options> o)
35         : _film (f)
36         , _opt (o)
37         , _just_skipped (false)
38         , _last_frame (0)
39 {
40
41 }
42
43
44 /** @return an estimate of the current number of frames we are encoding per second,
45  *  or 0 if not known.
46  */
47 float
48 Encoder::current_frames_per_second () const
49 {
50         boost::mutex::scoped_lock lock (_history_mutex);
51         if (int (_time_history.size()) < _history_size) {
52                 return 0;
53         }
54
55         struct timeval now;
56         gettimeofday (&now, 0);
57
58         return _history_size / (seconds (now) - seconds (_time_history.back ()));
59 }
60
61 /** @return true if the last frame to be processed was skipped as it already existed */
62 bool
63 Encoder::skipping () const
64 {
65         boost::mutex::scoped_lock (_history_mutex);
66         return _just_skipped;
67 }
68
69 /** @return Index of last frame to be successfully encoded */
70 int
71 Encoder::last_frame () const
72 {
73         boost::mutex::scoped_lock (_history_mutex);
74         return _last_frame;
75 }
76
77 /** Should be called when a frame has been encoded successfully.
78  *  @param n Frame index.
79  */
80 void
81 Encoder::frame_done (int n)
82 {
83         boost::mutex::scoped_lock lock (_history_mutex);
84         _just_skipped = false;
85         _last_frame = n;
86         
87         struct timeval tv;
88         gettimeofday (&tv, 0);
89         _time_history.push_front (tv);
90         if (int (_time_history.size()) > _history_size) {
91                 _time_history.pop_back ();
92         }
93 }
94
95 /** Called by a subclass when it has just skipped the processing
96     of a frame because it has already been done.
97 */
98 void
99 Encoder::frame_skipped ()
100 {
101         boost::mutex::scoped_lock lock (_history_mutex);
102         _just_skipped = true;
103 }