Fix build again.
[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,
32         int video_length,
33         int audio_sample_rate,
34         float frames_per_second,
35         int dcp_frames_per_second
36         )
37         : AudioVideoProcessor (log)
38         , _video_start (video_trim_start)
39         , _video_end (video_length - video_trim_end)
40         , _video_in (0)
41         , _audio_in (0)
42 {
43         FrameRateConversion frc (frames_per_second, dcp_frames_per_second);
44
45         if (frc.skip) {
46                 _video_start /= 2;
47                 _video_end /= 2;
48         } else if (frc.repeat) {
49                 _video_start *= 2;
50                 _video_end *= 2;
51         }
52
53         if (audio_sample_rate) {
54                 _audio_start = video_frames_to_audio_frames (_video_start, audio_sample_rate, frames_per_second);
55                 _audio_end = video_frames_to_audio_frames (_video_end, audio_sample_rate, frames_per_second);
56         }
57
58         /* XXX: this is a hack; this flag means that no trim is happening at the end of the film, and I'm
59            using that to prevent audio trim being rounded to video trim, which breaks the current set
60            of regression tests.  This could be removed if a) the regression tests are regenerated and b)
61            I can work out what DCP length should be.
62         */
63         _no_trim = (_video_start == 0) && (_video_end == (video_length - video_trim_end));
64 }
65
66 void
67 Trimmer::process_video (shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub)
68 {
69         if (_no_trim || (_video_in >= _video_start && _video_in <= _video_end)) {
70                 Video (image, same, sub);
71         }
72         
73         ++_video_in;
74 }
75
76 void
77 Trimmer::process_audio (shared_ptr<const AudioBuffers> audio)
78 {
79         if (_no_trim) {
80                 Audio (audio);
81                 return;
82         }
83         
84         int64_t offset = _audio_start - _audio_in;
85         if (offset > audio->frames()) {
86                 _audio_in += audio->frames ();
87                 return;
88         }
89
90         if (offset < 0) {
91                 offset = 0;
92         }
93
94         int64_t length = _audio_end - max (_audio_in, _audio_start);
95         if (length < 0) {
96                 _audio_in += audio->frames ();
97                 return;
98         }
99
100         if (length > (audio->frames() - offset)) {
101                 length = audio->frames () - offset;
102         }
103
104         _audio_in += audio->frames ();
105         
106         if (offset != 0 || length != audio->frames ()) {
107                 shared_ptr<AudioBuffers> copy (new AudioBuffers (audio));
108                 copy->move (offset, 0, length);
109                 copy->set_frames (length);
110                 audio = copy;
111         }
112         
113         Audio (audio);
114 }
115