summaryrefslogtreecommitdiff
path: root/src/lib/resampler_manager.cc
blob: 90ec1bfbcada39e9144c50b191e7274e2bc236c1 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "audio_content.h"
#include "audio_decoder.h"
#include "dcpomatic_log.h"
#include "resampler.h"
#include "resampler_manager.h"
#include <boost/foreach.hpp>
#include <vector>


using std::vector;
using std::shared_ptr;
using std::make_shared;
using namespace dcpomatic;


void
ResamplerManager::add (DCPTime start, DCPTime end, shared_ptr<AudioContent> content, shared_ptr<AudioDecoder> decoder)
{
	_groups.push_back (Group(start, end, content, decoder));
	coalesce ();
}


void
ResamplerManager::coalesce ()
{
	if (_groups.size() < 2) {
		return;
	}

	while (coalesce_pass()) {}
}


bool
ResamplerManager::can_share (Group const& a, Group const& b) const
{
	auto as = a.contents[0]->streams();
	auto bs = b.contents[0]->streams();

	if (a.contents[0]->streams().size() != b.contents[0]->streams().size()) {
		return false;
	}

	auto film = _film.lock ();
	DCPOMATIC_ASSERT (film);
	if (a.contents[0]->resampled_frame_rate(film) != b.contents[0]->resampled_frame_rate(film)) {
		return false;
	}

	/* The first, second, etc. streams in a and b must be the same */

	auto i = as.begin();
	auto j = bs.begin();

	while (i != as.end()) {
		if (
			(*i)->channels() != (*j)->channels() ||
			(*i)->mapping().mapped_channels() != (*j)->mapping().mapped_channels() ||
			(*i)->frame_rate() != (*j)->frame_rate()) {
			return false;
		}
		++i;
		++j;
	}

	return true;
}


bool
ResamplerManager::coalesce_pass ()
{
	for (size_t i = 0; i < _groups.size(); ++i) {
		for (size_t j = 0; j < _groups.size(); ++j) {
			if (i == j) {
				continue;
			}

			if (_groups[i].start == _groups[j].end && can_share(_groups[i], _groups[j])) {
				copy (_groups[i].decoders.begin(), _groups[i].decoders.end(), back_inserter(_groups[j].decoders));
				copy (_groups[i].contents.begin(), _groups[i].contents.end(), back_inserter(_groups[j].contents));
				_groups[j].end = _groups[i].end;
				_groups.erase (_groups.begin() + i);
				return true;
			}
		}
	}

	return false;
}


shared_ptr<Resampler>
ResamplerManager::get (AudioDecoder* decoder, AudioStreamPtr stream, bool fast)
{
	auto film = _film.lock();
	DCPOMATIC_ASSERT (film);

	for (auto& g: _groups) {
		for (size_t i = 0; i < g.decoders.size(); ++i) {
			if (g.decoders[i].get() == decoder) {
				auto content_streams = g.contents[i]->streams();
				auto index = find(content_streams.begin(), content_streams.end(), stream) - content_streams.begin();
				DCPOMATIC_ASSERT (index < static_cast<ptrdiff_t>(content_streams.size()));
				if (!g.resamplers[index] && stream->frame_rate() != g.contents[i]->resampled_frame_rate(film)) {
					LOG_GENERAL (
						"Creating new resampler from %1 to %2 with %3 channels",
						stream->frame_rate(),
						g.contents[i]->resampled_frame_rate(film),
						stream->channels()
						);

					g.resamplers[index] = make_shared<Resampler>(stream->frame_rate(), g.contents[i]->resampled_frame_rate(film), stream->channels());
					if (fast) {
						g.resamplers[index]->set_fast ();
					}
				}
				return g.resamplers[index];
			}
		}
	}

	DCPOMATIC_ASSERT (false);
	return {};
}


void
ResamplerManager::flush (AudioDecoder* decoder)
{
	for (auto& g: _groups) {
		if (find(g.decoders.begin(), g.decoders.end(), [decoder](shared_ptr<Decoder> d) { return d->audio.get() == decoder; }) != g.decoders.end()) {
			for (auto r: g.resamplers) {
				auto ro = r->flush ();
				/* XXX: err... which content do these samples belong to?  Won't
				 * the player throw the samples away if they appear to come from the
				 * wrong place?
				 */
			}
		}
	}
}