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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/* GStreamer
* Copyright (C) 2005 Wim Taymans <wim at fluendo dot com>
*
* audioconvert.c: Convert audio to different audio formats automatically
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "gstchannelmix.h"
#include "audioconvert.h"
/* int to float conversion: int2float(i) = 1 / (2^31-1) * i */
#define INT2FLOAT(i) (4.6566128752457969e-10 * ((gfloat)i))
/* sign bit in the intermediate format */
#define SIGNED (1<<31)
/***
* unpack code
*/
#define MAKE_UNPACK_FUNC_NAME(name) \
audio_convert_unpack_##name
#define MAKE_UNPACK_FUNC(name, stride, sign, READ_FUNC) \
static void \
MAKE_UNPACK_FUNC_NAME (name) (gpointer src, gint32 *dst, \
gint scale, gint count) \
{ \
guint8* p = (guint8 *) src; \
for (;count; count--) { \
*dst++ = (((gint32) READ_FUNC (p)) << scale) ^ (sign); \
p+=stride; \
} \
}
/* special unpack code for float */
static void
MAKE_UNPACK_FUNC_NAME (float) (gpointer src, gint32 * dst,
gint scale, gint count)
{
gfloat *p = (gfloat *) src;
gfloat temp;
for (; count; count--) {
temp = *p++ * 2147483647.0f + .5;
*dst++ = (gint32) CLAMP (temp, G_MININT, G_MAXINT);
}
}
#define READ8(p) GST_READ_UINT8(p)
#define READ16_FROM_LE(p) GST_READ_UINT16_LE (p)
#define READ16_FROM_BE(p) GST_READ_UINT16_BE (p)
#define READ24_FROM_LE(p) (p[0] | (p[1] << 8) | (p[2] << 16))
#define READ24_FROM_BE(p) (p[2] | (p[1] << 8) | (p[0] << 16))
#define READ32_FROM_LE(p) GST_READ_UINT32_LE (p)
#define READ32_FROM_BE(p) GST_READ_UINT32_BE (p)
MAKE_UNPACK_FUNC (u8, 1, SIGNED, READ8);
MAKE_UNPACK_FUNC (s8, 1, 0, READ8);
MAKE_UNPACK_FUNC (u16_le, 2, SIGNED, READ16_FROM_LE);
MAKE_UNPACK_FUNC (s16_le, 2, 0, READ16_FROM_LE);
MAKE_UNPACK_FUNC (u16_be, 2, SIGNED, READ16_FROM_BE);
MAKE_UNPACK_FUNC (s16_be, 2, 0, READ16_FROM_BE);
MAKE_UNPACK_FUNC (u24_le, 3, SIGNED, READ24_FROM_LE);
MAKE_UNPACK_FUNC (s24_le, 3, 0, READ24_FROM_LE);
MAKE_UNPACK_FUNC (u24_be, 3, SIGNED, READ24_FROM_BE);
MAKE_UNPACK_FUNC (s24_be, 3, 0, READ24_FROM_BE);
MAKE_UNPACK_FUNC (u32_le, 4, SIGNED, READ32_FROM_LE);
MAKE_UNPACK_FUNC (s32_le, 4, 0, READ32_FROM_LE);
MAKE_UNPACK_FUNC (u32_be, 4, SIGNED, READ32_FROM_BE);
MAKE_UNPACK_FUNC (s32_be, 4, 0, READ32_FROM_BE);
/***
* packing code
*/
#define MAKE_PACK_FUNC_NAME(name) \
audio_convert_pack_##name
#define MAKE_PACK_FUNC(name, stride, sign, WRITE_FUNC) \
static void \
MAKE_PACK_FUNC_NAME (name) (gint32 *src, gpointer dst, \
gint scale, gint count) \
{ \
guint8 *p = (guint8 *)dst; \
guint32 tmp; \
for (;count; count--) { \
tmp = (*src++ ^ (sign)) >> scale; \
WRITE_FUNC (p, tmp); \
p+=stride; \
} \
}
/* special float pack function */
static void
MAKE_PACK_FUNC_NAME (float) (gint32 * src, gpointer dst, gint scale, gint count)
{
gfloat *p = (gfloat *) dst;
for (; count; count--) {
*p++ = INT2FLOAT (*src++);
}
}
#define WRITE8(p, v) GST_WRITE_UINT8 (p, v)
#define WRITE16_TO_LE(p,v) GST_WRITE_UINT16_LE (p, (guint16)(v))
#define WRITE16_TO_BE(p,v) GST_WRITE_UINT16_BE (p, (guint16)(v))
#define WRITE24_TO_LE(p,v) p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff
#define WRITE24_TO_BE(p,v) p[2] = v & 0xff; p[1] = (v >> 8) & 0xff; p[0] = (v >> 16) & 0xff
#define WRITE32_TO_LE(p,v) GST_WRITE_UINT32_LE (p, (guint32)(v))
#define WRITE32_TO_BE(p,v) GST_WRITE_UINT32_BE (p, (guint32)(v))
MAKE_PACK_FUNC (u8, 1, SIGNED, WRITE8);
MAKE_PACK_FUNC (s8, 1, 0, WRITE8);
MAKE_PACK_FUNC (u16_le, 2, SIGNED, WRITE16_TO_LE);
MAKE_PACK_FUNC (s16_le, 2, 0, WRITE16_TO_LE);
MAKE_PACK_FUNC (u16_be, 2, SIGNED, WRITE16_TO_BE);
MAKE_PACK_FUNC (s16_be, 2, 0, WRITE16_TO_BE);
MAKE_PACK_FUNC (u24_le, 3, SIGNED, WRITE24_TO_LE);
MAKE_PACK_FUNC (s24_le, 3, 0, WRITE24_TO_LE);
MAKE_PACK_FUNC (u24_be, 3, SIGNED, WRITE24_TO_BE);
MAKE_PACK_FUNC (s24_be, 3, 0, WRITE24_TO_BE);
MAKE_PACK_FUNC (u32_le, 4, SIGNED, WRITE32_TO_LE);
MAKE_PACK_FUNC (s32_le, 4, 0, WRITE32_TO_LE);
MAKE_PACK_FUNC (u32_be, 4, SIGNED, WRITE32_TO_BE);
MAKE_PACK_FUNC (s32_be, 4, 0, WRITE32_TO_BE);
static AudioConvertUnpack unpack_funcs[] = {
MAKE_UNPACK_FUNC_NAME (u8),
MAKE_UNPACK_FUNC_NAME (s8),
MAKE_UNPACK_FUNC_NAME (u8),
MAKE_UNPACK_FUNC_NAME (s8),
MAKE_UNPACK_FUNC_NAME (u16_le),
MAKE_UNPACK_FUNC_NAME (s16_le),
MAKE_UNPACK_FUNC_NAME (u16_be),
MAKE_UNPACK_FUNC_NAME (s16_be),
MAKE_UNPACK_FUNC_NAME (u24_le),
MAKE_UNPACK_FUNC_NAME (s24_le),
MAKE_UNPACK_FUNC_NAME (u24_be),
MAKE_UNPACK_FUNC_NAME (s24_be),
MAKE_UNPACK_FUNC_NAME (u32_le),
MAKE_UNPACK_FUNC_NAME (s32_le),
MAKE_UNPACK_FUNC_NAME (u32_be),
MAKE_UNPACK_FUNC_NAME (s32_be),
MAKE_UNPACK_FUNC_NAME (float),
};
static AudioConvertPack pack_funcs[] = {
MAKE_PACK_FUNC_NAME (u8),
MAKE_PACK_FUNC_NAME (s8),
MAKE_PACK_FUNC_NAME (u8),
MAKE_PACK_FUNC_NAME (s8),
MAKE_PACK_FUNC_NAME (u16_le),
MAKE_PACK_FUNC_NAME (s16_le),
MAKE_PACK_FUNC_NAME (u16_be),
MAKE_PACK_FUNC_NAME (s16_be),
MAKE_PACK_FUNC_NAME (u24_le),
MAKE_PACK_FUNC_NAME (s24_le),
MAKE_PACK_FUNC_NAME (u24_be),
MAKE_PACK_FUNC_NAME (s24_be),
MAKE_PACK_FUNC_NAME (u32_le),
MAKE_PACK_FUNC_NAME (s32_le),
MAKE_PACK_FUNC_NAME (u32_be),
MAKE_PACK_FUNC_NAME (s32_be),
MAKE_PACK_FUNC_NAME (float),
};
static gint
audio_convert_get_func_index (AudioConvertFmt * fmt)
{
gint index = 0;
if (fmt->is_int) {
index += (fmt->width / 8 - 1) * 4;
index += fmt->endianness == G_LITTLE_ENDIAN ? 0 : 2;
index += fmt->sign ? 1 : 0;
} else {
index = 16;
}
return index;
}
static gboolean
check_default (AudioConvertFmt * fmt)
{
return (fmt->width == 32 && fmt->depth == 32 &&
fmt->endianness == G_BYTE_ORDER && fmt->sign == TRUE);
}
gboolean
audio_convert_clean_fmt (AudioConvertFmt * fmt)
{
g_return_val_if_fail (fmt != NULL, FALSE);
g_free (fmt->pos);
fmt->pos = NULL;
return TRUE;
}
gboolean
audio_convert_prepare_context (AudioConvertCtx * ctx, AudioConvertFmt * in,
AudioConvertFmt * out)
{
gint idx;
g_return_val_if_fail (ctx != NULL, FALSE);
g_return_val_if_fail (in != NULL, FALSE);
g_return_val_if_fail (out != NULL, FALSE);
/* first clean the existing context */
audio_convert_clean_context (ctx);
ctx->in = *in;
ctx->out = *out;
gst_channel_mix_setup_matrix (ctx);
idx = audio_convert_get_func_index (in);
if (!(ctx->unpack = unpack_funcs[idx]))
goto not_supported;
idx = audio_convert_get_func_index (out);
if (!(ctx->pack = pack_funcs[idx]))
goto not_supported;
/* check if input is in default format */
ctx->in_default = check_default (in);
/* check if channel mixer is passthrough */
ctx->mix_passthrough = gst_channel_mix_passthrough (ctx);
/* check if output is in default format */
ctx->out_default = check_default (out);
ctx->in_scale = 32 - in->depth;
ctx->out_scale = 32 - out->depth;
return TRUE;
not_supported:
{
return FALSE;
}
}
gboolean
audio_convert_clean_context (AudioConvertCtx * ctx)
{
g_return_val_if_fail (ctx != NULL, FALSE);
audio_convert_clean_fmt (&ctx->in);
audio_convert_clean_fmt (&ctx->out);
gst_channel_mix_unset_matrix (ctx);
g_free (ctx->tmpbuf);
ctx->tmpbuf = NULL;
ctx->tmpbufsize = 0;
return TRUE;
}
gboolean
audio_convert_get_sizes (AudioConvertCtx * ctx, gint samples, gint * srcsize,
gint * dstsize)
{
g_return_val_if_fail (ctx != NULL, FALSE);
if (srcsize)
*srcsize = samples * ctx->in.unit_size;
if (dstsize)
*dstsize = samples * ctx->out.unit_size;
return TRUE;
}
gboolean
audio_convert_convert (AudioConvertCtx * ctx, gpointer src,
gpointer dst, gint samples, gboolean src_writable)
{
gint insize, outsize;
gpointer outbuf, tmpbuf;
gint biggest = 0;
g_return_val_if_fail (ctx != NULL, FALSE);
g_return_val_if_fail (src != NULL, FALSE);
g_return_val_if_fail (dst != NULL, FALSE);
g_return_val_if_fail (samples >= 0, FALSE);
if (samples == 0)
return TRUE;
insize = ctx->in.unit_size * samples;
outsize = ctx->out.unit_size * samples;
/* find biggest temp buffer size */
if (!ctx->in_default)
biggest = insize * 32 / ctx->in.width;
if (!ctx->mix_passthrough)
biggest = MAX (biggest, outsize * 32 / ctx->out.width);
/* see if one of the buffers can be used as temp */
if (outsize >= biggest)
tmpbuf = dst;
else if (insize >= biggest && src_writable)
tmpbuf = src;
else {
if (biggest > ctx->tmpbufsize) {
ctx->tmpbuf = g_realloc (ctx->tmpbuf, biggest);
ctx->tmpbufsize = biggest;
}
tmpbuf = ctx->tmpbuf;
}
/* start conversion */
if (!ctx->in_default) {
/* check if final conversion */
if (!(ctx->out_default && ctx->mix_passthrough))
outbuf = tmpbuf;
else
outbuf = dst;
/* unpack to default format */
ctx->unpack (src, outbuf, ctx->in_scale, samples * ctx->in.channels);
src = outbuf;
}
if (!ctx->mix_passthrough) {
/* check if final conversion */
if (!ctx->out_default)
outbuf = tmpbuf;
else
outbuf = dst;
/* convert channels */
gst_channel_mix_mix (ctx, src, outbuf, samples);
src = outbuf;
}
if (!ctx->out_default) {
/* pack default format into dst */
ctx->pack (src, dst, ctx->out_scale, samples * ctx->out.channels);
}
return TRUE;
}
|