PcmExport.hxx 4.1 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#ifndef PCM_EXPORT_HXX
#define PCM_EXPORT_HXX
22 23

#include "check.h"
24
#include "PcmBuffer.hxx"
25
#include "AudioFormat.hxx"
26

27
template<typename T> struct ConstBuffer;
28 29 30 31 32 33

/**
 * An object that handles export of PCM samples to some instance
 * outside of MPD.  It has a few more options to tweak the binary
 * representation which are not supported by the pcm_convert library.
 */
34
struct PcmExport {
35 36
	struct Params {
		bool alsa_channel_order = false;
37
#ifdef ENABLE_DSD
38
		bool dsd_u32 = false;
39
		bool dop = false;
40
#endif
41 42 43
		bool shift8 = false;
		bool pack24 = false;
		bool reverse_endian = false;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

		/**
		 * Calculate the output sample rate, given a specific input
		 * sample rate.  Usually, both are the same; however, with
		 * DSD_U32, four input bytes (= 4 * 8 bits) are combined to
		 * one output word (32 bits), dividing the sample rate by 4.
		 */
		gcc_pure
		unsigned CalcOutputSampleRate(unsigned input_sample_rate) const;

		/**
		 * The inverse of CalcOutputSampleRate().
		 */
		gcc_pure
		unsigned CalcInputSampleRate(unsigned output_sample_rate) const;
59 60
	};

61 62 63 64 65 66 67
	/**
	 * This buffer is used to reorder channels.
	 *
	 * @see #alsa_channel_order
	 */
	PcmBuffer order_buffer;

68
#ifdef ENABLE_DSD
69 70
	/**
	 * The buffer is used to convert DSD samples to the
71
	 * DoP format.
72
	 *
73
	 * @see #dop
74
	 */
75
	PcmBuffer dop_buffer;
76
#endif
77

78
	/**
79
	 * The buffer is used to pack samples, removing padding.
80
	 *
81
	 * @see #pack24
82
	 */
83
	PcmBuffer pack_buffer;
84

85
	/**
86
	 * The buffer is used to reverse the byte order.
87 88 89
	 *
	 * @see #reverse_endian
	 */
90
	PcmBuffer reverse_buffer;
91

92 93 94 95 96
	/**
	 * The number of channels.
	 */
	uint8_t channels;

97 98 99 100 101 102 103 104 105 106
	/**
	 * Convert the given buffer from FLAC channel order to ALSA
	 * channel order using ToAlsaChannelOrder()?
	 *
	 * If this value is SampleFormat::UNDEFINED, then no channel
	 * reordering is applied, otherwise this is the input sample
	 * format.
	 */
	SampleFormat alsa_channel_order;

107
#ifdef ENABLE_DSD
108 109 110 111 112
	/**
	 * Convert DSD (U8) to DSD_U32?
	 */
	bool dsd_u32;

113
	/**
114
	 * Convert DSD to DSD-over-PCM (DoP)?  Input format must be
115 116
	 * SampleFormat::DSD and output format must be
	 * SampleFormat::S24_P32.
117
	 */
118
	bool dop;
119
#endif
120

121 122 123 124 125 126
	/**
	 * Convert (padded) 24 bit samples to 32 bit by shifting 8
	 * bits to the left?
	 */
	bool shift8;

127 128 129 130 131
	/**
	 * Pack 24 bit samples?
	 */
	bool pack24;

132
	/**
133 134 135
	 * Export the samples in reverse byte order?  A non-zero value
	 * means the option is enabled and represents the size of each
	 * sample (2 or bigger).
136
	 */
137
	uint8_t reverse_endian;
138

139
	/**
Max Kellermann's avatar
Max Kellermann committed
140
	 * Open the object.
141 142
	 *
	 * There is no "close" method.  This function may be called multiple
143
	 * times to reuse the object.
144 145 146
	 *
	 * This function cannot fail.
	 *
147
	 * @param channels the number of channels; ignored unless dop is set
148
	 */
149
	void Open(SampleFormat sample_format, unsigned channels,
150
		  Params params);
151

152 153 154 155
	/**
	 * Calculate the size of one output frame.
	 */
	gcc_pure
156
	size_t GetFrameSize(const AudioFormat &audio_format) const;
157

158 159 160 161 162 163
	/**
	 * Export a PCM buffer.
	 *
	 * @param src the source PCM buffer
	 * @return the destination buffer (may be a pointer to the source buffer)
	 */
164
	ConstBuffer<void> Export(ConstBuffer<void> src);
165

166 167 168 169 170 171 172 173
	/**
	 * Converts the number of consumed bytes from the pcm_export()
	 * destination buffer to the according number of bytes from the
	 * pcm_export() source buffer.
	 */
	gcc_pure
	size_t CalcSourceSize(size_t dest_size) const;
};
174

175
#endif