summaryrefslogtreecommitdiff
path: root/src/pcm/Dsd2Pcm.hxx
blob: 23f79a01be4abe2d3ce6036216d343d69e758dcc (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
/*

Copyright 2009, 2011 Sebastian Gesemann. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, this list of
      conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice, this list
      of conditions and the following disclaimer in the documentation and/or other materials
      provided with the distribution.

THIS SOFTWARE IS PROVIDED BY SEBASTIAN GESEMANN ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SEBASTIAN GESEMANN OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Sebastian Gesemann.

 */

#ifndef DSD2PCM_H_INCLUDED
#define DSD2PCM_H_INCLUDED

#include "ChannelDefs.hxx"

#include <array>
#include <cstddef>
#include <cstdint>

/**
 * A "dsd2pcm engine" for one channel.
 */
class Dsd2Pcm {
	friend class MultiDsd2Pcm;

public:
	/* must be a power of two */
	static constexpr size_t FIFOSIZE = 16;

private:
	/** bit mask for FIFO offsets */
	static constexpr size_t FIFOMASK = FIFOSIZE - 1;

	uint8_t fifo[FIFOSIZE];
	size_t fifopos;

public:
	Dsd2Pcm() noexcept {
		Reset();
	}

	/**
	 * resets the internal state for a fresh new stream
	 */
	void Reset() noexcept;

	/**
	 * "translates" a stream of octets to a stream of floats
	 * (8:1 decimation)
	 * @param ctx -- pointer to abstract context (buffers)
	 * @param samples -- number of octets/samples to "translate"
	 * @param src -- pointer to first octet (input)
	 * @param src_stride -- src pointer increment
	 * @param dst -- pointer to first float (output)
	 * @param dst_stride -- dst pointer increment
	 */
	void Translate(size_t samples,
		       const uint8_t *src, ptrdiff_t src_stride,
		       float *dst, ptrdiff_t dst_stride) noexcept;

	void TranslateS24(size_t samples,
			  const uint8_t *src, ptrdiff_t src_stride,
			  int32_t *dst, ptrdiff_t dst_stride) noexcept;

private:
	void ApplySample(size_t ffp, uint8_t src) noexcept;
	float CalcOutputSample(size_t ffp) const noexcept;
	float TranslateSample(size_t ffp, uint8_t src) noexcept;

	int32_t CalcOutputSampleS24(size_t ffp) const noexcept;
	int32_t TranslateSampleS24(size_t ffp, uint8_t src) noexcept;
};

class MultiDsd2Pcm {
	std::array<Dsd2Pcm, MAX_CHANNELS> per_channel;

	size_t fifopos = 0;

public:
	void Reset() noexcept {
		for (auto &i : per_channel)
			i.Reset();
		fifopos = 0;
	}

	void Translate(unsigned channels, size_t n_frames,
		       const uint8_t *src, float *dest) noexcept;

	void TranslateS24(unsigned channels, size_t n_frames,
			  const uint8_t *src, int32_t *dest) noexcept;

private:
	/**
	 * Optimized implementation for the common case.
	 */
	void TranslateStereo(size_t n_frames,
			     const uint8_t *src, float *dest) noexcept;

	void TranslateStereoS24(size_t n_frames,
				const uint8_t *src, int32_t *dest) noexcept;
};

#endif /* include guard DSD2PCM_H_INCLUDED */