summaryrefslogtreecommitdiff
path: root/src/lib/trimmer.cc
blob: 1ec9e2a5b70c6571b9646859cf5c937ff390a085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
    Copyright (C) 2013 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <boost/shared_ptr.hpp>
#include <stdint.h>
#include "trimmer.h"

using std::cout;
using std::max;
using boost::shared_ptr;

/** @param audio_sample_rate Audio sampling rate, or 0 if there is no audio */
Trimmer::Trimmer (
	shared_ptr<Log> log,
	int video_trim_start,
	int video_trim_end,
	int video_length,
	int audio_sample_rate,
	float frames_per_second,
	int dcp_frames_per_second
	)
	: AudioVideoProcessor (log)
	, _video_start (video_trim_start)
	, _video_end (video_length - video_trim_end)
	, _video_in (0)
	, _audio_in (0)
{
	FrameRateConversion frc (frames_per_second, dcp_frames_per_second);

	if (frc.skip) {
		_video_start /= 2;
		_video_end /= 2;
	} else if (frc.repeat) {
		_video_start *= 2;
		_video_end *= 2;
	}

	if (audio_sample_rate) {
		_audio_start = video_frames_to_audio_frames (_video_start, audio_sample_rate, frames_per_second);
		_audio_end = video_frames_to_audio_frames (_video_end, audio_sample_rate, frames_per_second);
	}

	/* XXX: this is a hack; if there is no trim at the end, set
	   the audio end point to infinity so that
	   shorter-video-than-audio does not trim audio (which breaks
	   the current set of regression tests).  This could be
	   removed if a) the regression tests are regenerated and b) I
	   can work out what DCP length should be.
	*/
	if (video_trim_end == 0) {
		_audio_end = INT64_MAX;
	}
}

void
Trimmer::process_video (shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub)
{
	if (_video_in >= _video_start && _video_in < _video_end) {
		Video (image, same, sub);
	}
	
	++_video_in;
}

void
Trimmer::process_audio (shared_ptr<const AudioBuffers> audio)
{
	int64_t offset = _audio_start - _audio_in;
	if (offset > audio->frames()) {
		/* we haven't reached the start of the untrimmed section yet */
		_audio_in += audio->frames ();
		return;
	}

	if (offset < 0) {
		offset = 0;
	}

	int64_t length = _audio_end - max (_audio_in, _audio_start);
	if (length < 0) {
		_audio_in += audio->frames ();
		return;
	}

	if (length > (audio->frames() - offset)) {
		length = audio->frames () - offset;
	}

	_audio_in += audio->frames ();
	
	if (offset != 0 || length != audio->frames ()) {
		shared_ptr<AudioBuffers> copy (new AudioBuffers (audio));
		copy->move (offset, 0, length);
		copy->set_frames (length);
		audio = copy;
	}
	
	Audio (audio);
}