Fix quoting for ffprobe.
[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 <stdint.h>
22 #include "trimmer.h"
23
24 using std::cout;
25 using std::max;
26 using boost::shared_ptr;
27
28 /** @param audio_sample_rate Audio sampling rate, or 0 if there is no audio */
29 Trimmer::Trimmer (
30         shared_ptr<Log> log,
31         int video_trim_start,
32         int video_trim_end,
33         int video_length,
34         int audio_sample_rate,
35         float frames_per_second,
36         int dcp_frames_per_second
37         )
38         : AudioVideoProcessor (log)
39         , _video_start (video_trim_start)
40         , _video_end (video_length - video_trim_end)
41         , _video_in (0)
42         , _audio_in (0)
43 {
44         FrameRateConversion frc (frames_per_second, dcp_frames_per_second);
45
46         if (frc.skip) {
47                 _video_start /= 2;
48                 _video_end /= 2;
49         } else if (frc.repeat) {
50                 _video_start *= 2;
51                 _video_end *= 2;
52         }
53
54         if (audio_sample_rate) {
55                 _audio_start = video_frames_to_audio_frames (_video_start, audio_sample_rate, frames_per_second);
56                 _audio_end = video_frames_to_audio_frames (_video_end, audio_sample_rate, frames_per_second);
57         }
58
59         /* XXX: this is a hack; if there is no trim at the end, set
60            the audio end point to infinity so that
61            shorter-video-than-audio does not trim audio (which breaks
62            the current set of regression tests).  This could be
63            removed if a) the regression tests are regenerated and b) I
64            can work out what DCP length should be.
65         */
66         if (video_trim_end == 0) {
67                 _audio_end = INT64_MAX;
68         }
69 }
70
71 void
72 Trimmer::process_video (shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub)
73 {
74         if (_video_in >= _video_start && _video_in < _video_end) {
75                 Video (image, same, sub);
76         }
77         
78         ++_video_in;
79 }
80
81 void
82 Trimmer::process_audio (shared_ptr<const AudioBuffers> audio)
83 {
84         int64_t offset = _audio_start - _audio_in;
85         if (offset > audio->frames()) {
86                 /* we haven't reached the start of the untrimmed section yet */
87                 _audio_in += audio->frames ();
88                 return;
89         }
90
91         if (offset < 0) {
92                 offset = 0;
93         }
94
95         int64_t length = _audio_end - max (_audio_in, _audio_start);
96         if (length < 0) {
97                 _audio_in += audio->frames ();
98                 return;
99         }
100
101         if (length > (audio->frames() - offset)) {
102                 length = audio->frames () - offset;
103         }
104
105         _audio_in += audio->frames ();
106         
107         if (offset != 0 || length != audio->frames ()) {
108                 shared_ptr<AudioBuffers> copy (new AudioBuffers (audio));
109                 copy->move (offset, 0, length);
110                 copy->set_frames (length);
111                 audio = copy;
112         }
113         
114         Audio (audio);
115 }
116