This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathalsaplugin.c
249 lines (211 loc) · 9.04 KB
/
alsaplugin.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* This file is part of dcaenc.
*
* Copyright (c) 2008-2012 Alexander E. Patrakov <[email protected]>
*
* dcaenc is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* dcaenc 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with dcaenc; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dcaenc.h"
#include <stdint.h>
#include <alsa/asoundlib.h>
#include <alsa/pcm_external.h>
struct dcaplug_info {
snd_pcm_extplug_t ext;
int iec61937;
dcaenc_context enc;
int32_t pcm_buffer[6 * 512];
int16_t dts_buffer[2 * 512];
snd_pcm_uframes_t bufpos;
};
static inline void *area_addr(const snd_pcm_channel_area_t *area,
snd_pcm_uframes_t offset)
{
unsigned int bitofs = area->first + area->step * offset;
return (char *) area->addr + bitofs / 8;
}
static inline int32_t get_s32(const snd_pcm_channel_area_t *area,
snd_pcm_uframes_t offset, snd_pcm_format_t format)
{
int32_t* sample32;
int16_t* sample16;
switch (format) {
case SND_PCM_FORMAT_S32:
sample32 = (int32_t*)area_addr(area, offset);
return *sample32;
case SND_PCM_FORMAT_S16:
sample16 = (int16_t*)area_addr(area, offset);
return *sample16 * 0x10000;
}
return 0;
}
static inline void put_s16(const snd_pcm_channel_area_t *area,
snd_pcm_uframes_t offset, int16_t value)
{
int16_t* sample16 = (int16_t*)area_addr(area, offset);
*sample16 = value;
}
static snd_pcm_sframes_t dcaplug_transfer(snd_pcm_extplug_t *ext,
const snd_pcm_channel_area_t *dst_areas, snd_pcm_uframes_t dst_offset,
const snd_pcm_channel_area_t *src_areas, snd_pcm_uframes_t src_offset,
snd_pcm_uframes_t size)
{
struct dcaplug_info *dcaplug = (struct dcaplug_info*)ext;
/* Force samples to the buffer */
snd_pcm_uframes_t remaining = 512 - dcaplug->bufpos;
if (size > remaining)
size = remaining;
snd_pcm_uframes_t i;
int channel;
int srcbufidx = ext->channels * dcaplug->bufpos;
int dstbufidx = 2 * dcaplug->bufpos;
for (i = 0; i < size; i++) {
if (ext->channels == 4) {
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[0], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[1], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[2], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[3], i + src_offset, ext->format);
} else {
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[4], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[0], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[1], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[2], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[3], i + src_offset, ext->format);
dcaplug->pcm_buffer[srcbufidx++] = get_s32(&src_areas[5], i + src_offset, ext->format);
}
put_s16(&dst_areas[0], i + dst_offset, dcaplug->dts_buffer[dstbufidx++]);
put_s16(&dst_areas[1], i + dst_offset, dcaplug->dts_buffer[dstbufidx++]);
}
dcaplug->bufpos += size;
if (dcaplug->bufpos == 512) {
dcaenc_convert_s32(dcaplug->enc, dcaplug->pcm_buffer, (uint8_t*)dcaplug->dts_buffer);
dcaplug->bufpos = 0;
}
return size;
}
static const int32_t zero[512 * 6];
static int dcaplug_init(snd_pcm_extplug_t *ext)
{
struct dcaplug_info *dcaplug = (struct dcaplug_info*)ext;
if (ext->rate != 44100 && ext->rate != 48000) {
SNDERR("Wrong sample rate, must be 44100 or 48000 Hz");
return -EINVAL;
}
if (ext->channels == 2) {
/* TODO: passthrough? */
SNDERR("Conversion from stereo to DTS is pointless");
return -EINVAL;
}
if (ext->channels != 4 && ext->channels != 6) {
SNDERR("Wrong number of channels");
return -EINVAL;
}
if (dcaplug->iec61937) {
dcaplug->enc = dcaenc_create(ext->rate,
(ext->channels == 4) ? DCAENC_CHANNELS_2FRONT_2REAR : DCAENC_CHANNELS_3FRONT_2REAR,
ext->rate * 503 / 16, /* same as DVD */
((ext->channels == 4) ? 0 : DCAENC_FLAG_LFE) | DCAENC_FLAG_IEC_WRAP);
} else {
dcaplug->enc = dcaenc_create(ext->rate,
(ext->channels == 4) ? DCAENC_CHANNELS_2FRONT_2REAR : DCAENC_CHANNELS_3FRONT_2REAR,
ext->rate * 32, /* same as S16 stereo on a CD */
(ext->channels == 4) ? 0 : DCAENC_FLAG_LFE);
}
if (!dcaplug->enc) {
SNDERR("Failed to create DCA encoder");
return -ENOMEM;
}
if (dcaenc_output_size(dcaplug->enc) != 2048) {
SNDERR("The dcaenc library is incompatible");
return -EINVAL;
}
/* Create a dummy frame of silence */
dcaenc_convert_s32(dcaplug->enc, zero, (uint8_t*)dcaplug->dts_buffer);
return 0;
}
static int dcaplug_close(snd_pcm_extplug_t *ext)
{
struct dcaplug_info *dcaplug = (struct dcaplug_info*)ext;
dcaenc_destroy(dcaplug->enc, NULL);
dcaplug->enc = NULL;
return 0;
}
static const snd_pcm_extplug_callback_t dcaplug_callback = {
.transfer = dcaplug_transfer,
.init = dcaplug_init,
.close = dcaplug_close,
};
SND_PCM_PLUGIN_DEFINE_FUNC(dca)
{
snd_config_iterator_t i, next;
snd_config_t *slave = NULL;
int iec61937 = 0;
struct dcaplug_info *dcaplug;
int err;
if (stream != SND_PCM_STREAM_PLAYBACK) {
SNDERR("dca is only for playback");
return -EINVAL;
}
snd_config_for_each(i, next, conf) {
snd_config_t *n = snd_config_iterator_entry(i);
const char *id;
if (snd_config_get_id(n, &id) < 0)
continue;
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
continue;
if (strcmp(id, "slave") == 0) {
slave = n;
continue;
}
if (strcmp(id, "iec61937") == 0) {
if ((err = snd_config_get_bool(n)) < 0) {
SNDERR("Invalid value for %s", id);
return -EINVAL;
}
iec61937 = err;
continue;
}
SNDERR("Unknown field %s", id);
return -EINVAL;
}
if (!slave) {
SNDERR("No slave defined for dca");
return -EINVAL;
}
dcaplug = calloc(1, sizeof(*dcaplug));
if (dcaplug == NULL)
return -ENOMEM;
dcaplug->ext.version = SND_PCM_EXTPLUG_VERSION;
dcaplug->ext.name = "DTS Coherent Acoustics encoder";
dcaplug->ext.callback = &dcaplug_callback;
dcaplug->ext.private_data = dcaplug;
dcaplug->iec61937 = iec61937;
err = snd_pcm_extplug_create(&dcaplug->ext, name, root, slave, stream, mode);
if (err < 0) {
dcaenc_destroy(dcaplug->enc, NULL);
free(dcaplug);
return err;
}
static const int channels[2] = {4, 6};
static const int formats[2] = {SND_PCM_FORMAT_S32, SND_PCM_FORMAT_S16};
snd_pcm_extplug_set_param_list(&dcaplug->ext, SND_PCM_EXTPLUG_HW_CHANNELS,
2, channels);
snd_pcm_extplug_set_slave_param(&dcaplug->ext, SND_PCM_EXTPLUG_HW_CHANNELS, 2);
snd_pcm_extplug_set_param_list(&dcaplug->ext, SND_PCM_EXTPLUG_HW_FORMAT,
2, formats);
snd_pcm_extplug_set_slave_param(&dcaplug->ext, SND_PCM_EXTPLUG_HW_FORMAT, SND_PCM_FORMAT_S16);
*pcmp = dcaplug->ext.pcm;
return 0;
}
SND_PCM_PLUGIN_SYMBOL(dca);