Merge master.
[dcpomatic.git] / src / lib / trimmer.cc
1 /*
2     Copyright (C) 2013 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 #include <boost/shared_ptr.hpp>
21 #include "trimmer.h"
22
23 using std::cout;
24 using std::max;
25 using boost::shared_ptr;
26
27 /** @param audio_sample_rate Audio sampling rate, or 0 if there is no audio */
28 Trimmer::Trimmer (
29         shared_ptr<Log> log,
30         int video_trim_start,
31         int video_trim_end, int video_length,
32         int audio_sample_rate,
33         float frames_per_second,
34         int dcp_frames_per_second
35         )
36         : AudioVideoProcessor (log)
37         , _video_start (video_trim_start)
38         , _video_end (video_length - video_trim_end)
39         , _video_in (0)
40         , _audio_in (0)
41 {
42         FrameRateConversion frc (frames_per_second, dcp_frames_per_second);
43
44         if (frc.skip) {
45                 _video_start /= 2;
46                 _video_end /= 2;
47         } else if (frc.repeat) {
48                 _video_start *= 2;
49                 _video_end *= 2;
50         }
51
52         if (audio_sample_rate) {
53                 _audio_start = video_frames_to_audio_frames (_video_start, audio_sample_rate, frames_per_second);
54                 _audio_end = video_frames_to_audio_frames (_video_end, audio_sample_rate, frames_per_second);
55         }
56 }
57
58 void
59 Trimmer::process_video (shared_ptr<Image> image, bool same, shared_ptr<Subtitle> sub)
60 {
61         if (_video_in >= _video_start && _video_in <= _video_end) {
62                 Video (image, same, sub);
63         }
64         
65         ++_video_in;
66 }
67
68 void
69 Trimmer::process_audio (shared_ptr<AudioBuffers> audio)
70 {
71         int64_t offset = _audio_start - _audio_in;
72         if (offset > audio->frames()) {
73                 _audio_in += audio->frames ();
74                 return;
75         }
76
77         if (offset < 0) {
78                 offset = 0;
79         }
80
81         int64_t length = _audio_end - max (_audio_in, _audio_start);
82         if (length < 0) {
83                 _audio_in += audio->frames ();
84                 return;
85         }
86
87         if (length > (audio->frames() - offset)) {
88                 length = audio->frames () - offset;
89         }
90
91         _audio_in += audio->frames ();
92         
93         if (offset != 0 || length != audio->frames ()) {
94                 audio->move (offset, 0, length);
95                 audio->set_frames (length);
96         }
97         
98         Audio (audio);
99 }
100