2
* Copyright (C) 2003-2013 The Music Player Daemon Project
3
* http://www.musicpd.org
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License along
16
* with this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
#include "PcmDsdUsb.hxx"
22
#include "PcmBuffer.hxx"
23
#include "AudioFormat.hxx"
26
static inline uint32_t
27
pcm_two_dsd_to_usb_marker1(uint8_t a, uint8_t b)
29
return 0xff050000 | (a << 8) | b;
33
static inline uint32_t
34
pcm_two_dsd_to_usb_marker2(uint8_t a, uint8_t b)
36
return 0xfffa0000 | (a << 8) | b;
41
pcm_dsd_to_usb(PcmBuffer &buffer, unsigned channels,
42
const uint8_t *src, size_t src_size,
45
assert(audio_valid_channel_count(channels));
48
assert(src_size % channels == 0);
50
const unsigned num_src_samples = src_size;
51
const unsigned num_src_frames = num_src_samples / channels;
53
/* this rounds down and discards the last odd frame; not
54
elegant, but good enough for now */
55
const unsigned num_frames = num_src_frames / 2;
56
const unsigned num_samples = num_frames * channels;
58
const size_t dest_size = num_samples * 4;
59
*dest_size_r = dest_size;
60
uint32_t *const dest0 = (uint32_t *)buffer.Get(dest_size),
63
for (unsigned i = num_frames / 2; i > 0; --i) {
64
for (unsigned c = channels; c > 0; --c) {
65
/* each 24 bit sample has 16 DSD sample bits
66
plus the magic 0x05 marker */
68
*dest++ = pcm_two_dsd_to_usb_marker1(src[0], src[channels]);
70
/* seek the source pointer to the next
75
/* skip the second byte of each channel, because we
76
have already copied it */
79
for (unsigned c = channels; c > 0; --c) {
80
/* each 24 bit sample has 16 DSD sample bits
81
plus the magic 0xfa marker */
83
*dest++ = pcm_two_dsd_to_usb_marker2(src[0], src[channels]);
85
/* seek the source pointer to the next
90
/* skip the second byte of each channel, because we
91
have already copied it */