RestBuffer.hxx 3.91 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * http://www.musicpd.org
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_PCM_REST_BUFFER_HXX
#define MPD_PCM_REST_BUFFER_HXX

#include "ChannelDefs.hxx"

#include <algorithm>
26
#include <cassert>
27
#include <cstddef>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

template<typename T> struct ConstBuffer;
class PcmBuffer;

/**
 * A buffer which helps with conversion classes which need to handle
 * multiple frames at a time; it stores the rest of a previous
 * operation in order to use it in the next call.
 */
template<typename T, unsigned n_frames>
class PcmRestBuffer {
	unsigned size, capacity;
	T data[n_frames * MAX_CHANNELS];

public:
	void Open(unsigned channels) noexcept {
		assert(audio_valid_channel_count(channels));

		capacity = n_frames * channels;
		size = 0;
	}

50 51 52 53 54 55 56
	/**
	 * @return the size of one input block in #T samples
	 */
	size_t GetInputBlockSize() const noexcept {
		return capacity;
	}

57 58 59 60 61 62 63 64 65 66
	unsigned GetChannelCount() const noexcept {
		return capacity / n_frames;
	}

	void Reset() noexcept {
		assert(audio_valid_channel_count(capacity / n_frames));

		size = 0;
	}

67
private:
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
	ConstBuffer<T> Complete(ConstBuffer<T> &src) noexcept {
		assert(audio_valid_channel_count(GetChannelCount()));
		assert(src.size % GetChannelCount() == 0);

		if (size == 0)
			return nullptr;

		size_t missing = capacity - size;
		size_t n = std::min(missing, src.size);
		std::copy_n(src.begin(), n, &data[size]);
		src.skip_front(n);
		size += n;

		if (size < capacity)
			return nullptr;

		size = 0;
		return {data, capacity};
	}

	void Append(ConstBuffer<T> src) noexcept {
		assert(audio_valid_channel_count(GetChannelCount()));
		assert(src.size % GetChannelCount() == 0);
		assert(size + src.size < capacity);

		std::copy_n(src.begin(), src.size, &data[size]);
		size += src.size;
	}

97
public:
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 145
	/**
	 * A helper function which attempts to complete the rest
	 * buffer, allocates a destination buffer and invokes the
	 * given function for both the rest buffer and the new source
	 * buffer.  In the end, it copies more remaining data to the
	 * rest buffer.
	 *
	 * @param U the destination data type
	 * @param F the inner process function
	 * @param dest_block_size how many destination samples in one block?
	 * @return the destination buffer (allocated from #buffer);
	 * may be empty
	 */
	template<typename U, typename F>
	ConstBuffer<U> Process(PcmBuffer &buffer, ConstBuffer<T> src,
			       size_t dest_block_size,
			       F &&f) {
		assert(dest_block_size % GetChannelCount() == 0);

		const auto previous_rest = Complete(src);
		assert(previous_rest.size == 0 ||
		       previous_rest.size == capacity);

		const size_t previous_rest_blocks = !previous_rest.empty();
		const size_t src_blocks = src.size / capacity;
		const size_t next_rest_samples = src.size % capacity;
		const size_t dest_blocks = previous_rest_blocks + src_blocks;
		const size_t dest_samples = dest_blocks * dest_block_size;

		const auto dest0 = buffer.GetT<U>(dest_samples);
		auto dest = dest0;

		if (!previous_rest.empty()) {
			f(dest, previous_rest.data, previous_rest_blocks);
			dest += dest_block_size;
		}

		f(dest, src.data, src_blocks);

		if (next_rest_samples > 0)
			Append({src.data + src_blocks * capacity,
				next_rest_samples});

		return { dest0, dest_samples };
	}
};

#endif