2 * Copyright (C) 2005 Wim Taymans <wim at fluendo dot com>
4 * audioconvert.c: Convert audio to different audio formats automatically
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
28 #include "gstchannelmix.h"
29 #include "audioconvert.h"
31 /* int to float conversion: int2float(i) = 1 / (2^31-1) * i */
32 #define INT2FLOAT(i) (4.6566128752457969e-10 * ((gfloat)i))
34 #define UNPACK_CODE(type, corr, E_FUNC) \
35 type* p = (type *) src; \
37 for (;count; count--) { \
38 tmp = ((gint64) E_FUNC (*p) - corr) * scale; \
39 *dst++ = CLAMP (tmp, -((gint64) 1 << 32), (gint64) 0x7FFFFFFF); \
43 #define MAKE_UNPACK_FUNC_NAME(name) \
44 audio_convert_unpack_##name
47 #define MAKE_UNPACK_FUNC_U(name, type, E_FUNC) \
49 MAKE_UNPACK_FUNC_NAME (name) (gpointer src, gint32 *dst, \
50 gint64 scale, gint count) \
52 UNPACK_CODE(type, (1 << (sizeof (type) * 8 - 1)), E_FUNC); \
56 #define MAKE_UNPACK_FUNC_S(name, type, E_FUNC) \
58 MAKE_UNPACK_FUNC_NAME (name) (gpointer src, gint32 *dst, \
59 gint64 scale, gint count) \
61 UNPACK_CODE(type, 0, E_FUNC); \
64 MAKE_UNPACK_FUNC_U (u8, guint8, /* nothing */ )
65 MAKE_UNPACK_FUNC_S (s8, gint8, /* nothing */ )
66 MAKE_UNPACK_FUNC_U (u16_le, guint16, GUINT16_FROM_LE)
67 MAKE_UNPACK_FUNC_S (s16_le, gint16, GINT16_FROM_LE)
68 MAKE_UNPACK_FUNC_U (u16_be, guint16, GUINT16_FROM_BE)
69 MAKE_UNPACK_FUNC_S (s16_be, gint16, GINT16_FROM_BE)
70 MAKE_UNPACK_FUNC_U (u32_le, guint32, GUINT32_FROM_LE)
71 MAKE_UNPACK_FUNC_S (s32_le, gint32, GINT32_FROM_LE)
72 MAKE_UNPACK_FUNC_U (u32_be, guint32, GUINT32_FROM_BE)
73 MAKE_UNPACK_FUNC_S (s32_be, gint32, GINT32_FROM_BE)
81 /* Read 24-bits LE/BE into signed 64 host-endian */
82 if (this->sinkcaps.endianness == G_LITTLE_ENDIAN)
84 cur = src[0] | (src[1] << 8) | (src[2] << 16);
86 cur = src[2] | (src[1] << 8) | (src[0] << 16);
90 if ((this->sinkcaps.sign)
91 && (cur & (1 << (this->sinkcaps.depth - 1))))
92 cur |= ((gint64) (-1)) ^ ((1 << this->sinkcaps.depth) - 1);
98 MAKE_UNPACK_FUNC_NAME (float) (gpointer src, gint32 * dst,
99 gint64 scale, gint count)
101 gfloat *p = (gfloat *) src;
104 for (; count; count--) {
105 temp = *p++ * 2147483647.0f + .5;
106 *dst++ = (gint32) CLAMP (temp, -2147483648ll, 2147483647ll);
110 #define PACK_CODE(type, corr, E_FUNC) \
111 type* p = (type *) dst; \
112 gint32 scale = (32 - depth); \
113 for (;count; count--) { \
114 *p = E_FUNC ((type)((*src) >> scale) + corr); \
118 #define MAKE_PACK_FUNC_NAME(name) \
119 audio_convert_pack_##name
121 #define MAKE_PACK_FUNC_U(name, type, E_FUNC) \
123 MAKE_PACK_FUNC_NAME (name) (gint32 *src, gpointer dst, \
124 gint depth, gint count) \
126 PACK_CODE (type, (1 << (depth - 1)), E_FUNC); \
129 #define MAKE_PACK_FUNC_S(name, type, E_FUNC) \
131 MAKE_PACK_FUNC_NAME (name) (gint32 *src, gpointer dst, \
132 gint depth, gint count) \
134 PACK_CODE (type, 0, E_FUNC); \
137 MAKE_PACK_FUNC_U (u8, guint8, /* nothing */ );
138 MAKE_PACK_FUNC_S (s8, gint8, /* nothing */ );
139 MAKE_PACK_FUNC_U (u16_le, guint16, GUINT16_TO_LE);
140 MAKE_PACK_FUNC_S (s16_le, gint16, GINT16_TO_LE);
141 MAKE_PACK_FUNC_U (u16_be, guint16, GUINT16_TO_BE);
142 MAKE_PACK_FUNC_S (s16_be, gint16, GINT16_TO_BE);
143 MAKE_PACK_FUNC_U (u32_le, guint32, GUINT32_TO_LE);
144 MAKE_PACK_FUNC_S (s32_le, gint32, GINT32_TO_LE);
145 MAKE_PACK_FUNC_U (u32_be, guint32, GUINT32_TO_BE);
146 MAKE_PACK_FUNC_S (s32_be, gint32, GINT32_TO_BE);
149 MAKE_PACK_FUNC_NAME (float) (gint32 * src, gpointer dst, gint depth, gint count)
151 gfloat *p = (gfloat *) dst;
153 for (; count; count--) {
154 *p++ = INT2FLOAT (*src++);
158 static AudioConvertUnpack unpack_funcs[] = {
159 MAKE_UNPACK_FUNC_NAME (u8),
160 MAKE_UNPACK_FUNC_NAME (s8),
161 MAKE_UNPACK_FUNC_NAME (u8),
162 MAKE_UNPACK_FUNC_NAME (s8),
163 MAKE_UNPACK_FUNC_NAME (u16_le),
164 MAKE_UNPACK_FUNC_NAME (s16_le),
165 MAKE_UNPACK_FUNC_NAME (u16_be),
166 MAKE_UNPACK_FUNC_NAME (s16_be),
171 MAKE_UNPACK_FUNC_NAME (u32_le),
172 MAKE_UNPACK_FUNC_NAME (s32_le),
173 MAKE_UNPACK_FUNC_NAME (u32_be),
174 MAKE_UNPACK_FUNC_NAME (s32_be),
175 MAKE_UNPACK_FUNC_NAME (float),
178 static AudioConvertPack pack_funcs[] = {
179 MAKE_PACK_FUNC_NAME (u8),
180 MAKE_PACK_FUNC_NAME (s8),
181 MAKE_PACK_FUNC_NAME (u8),
182 MAKE_PACK_FUNC_NAME (s8),
183 MAKE_PACK_FUNC_NAME (u16_le),
184 MAKE_PACK_FUNC_NAME (s16_le),
185 MAKE_PACK_FUNC_NAME (u16_be),
186 MAKE_PACK_FUNC_NAME (s16_be),
191 MAKE_PACK_FUNC_NAME (u32_le),
192 MAKE_PACK_FUNC_NAME (s32_le),
193 MAKE_PACK_FUNC_NAME (u32_be),
194 MAKE_PACK_FUNC_NAME (s32_be),
195 MAKE_PACK_FUNC_NAME (float),
199 audio_convert_get_func_index (AudioConvertFmt * fmt)
204 index += (fmt->width / 8 - 1) * 4;
205 index += fmt->endianness == G_LITTLE_ENDIAN ? 0 : 2;
206 index += fmt->sign ? 1 : 0;
214 check_default (AudioConvertFmt * fmt)
216 return (fmt->width == 32 && fmt->depth == 32 &&
217 fmt->endianness == G_BYTE_ORDER && fmt->sign == TRUE);
221 audio_convert_clean_fmt (AudioConvertFmt * fmt)
223 g_return_val_if_fail (fmt != NULL, FALSE);
233 audio_convert_prepare_context (AudioConvertCtx * ctx, AudioConvertFmt * in,
234 AudioConvertFmt * out)
238 g_return_val_if_fail (ctx != NULL, FALSE);
239 g_return_val_if_fail (in != NULL, FALSE);
240 g_return_val_if_fail (out != NULL, FALSE);
242 /* first clean the existing context */
243 audio_convert_clean_context (ctx);
248 gst_channel_mix_setup_matrix (ctx);
250 idx = audio_convert_get_func_index (in);
251 if (!(ctx->unpack = unpack_funcs[idx]))
254 idx = audio_convert_get_func_index (out);
255 if (!(ctx->pack = pack_funcs[idx]))
258 /* check if input is in default format */
259 ctx->in_default = check_default (in);
260 /* check if channel mixer is passthrough */
261 ctx->mix_passthrough = gst_channel_mix_passthrough (ctx);
262 /* check if output is in default format */
263 ctx->out_default = check_default (out);
265 ctx->scale = ((gint64) 1 << (32 - in->depth));
266 ctx->depth = out->depth;
277 audio_convert_clean_context (AudioConvertCtx * ctx)
279 g_return_val_if_fail (ctx != NULL, FALSE);
281 audio_convert_clean_fmt (&ctx->in);
282 audio_convert_clean_fmt (&ctx->out);
283 gst_channel_mix_unset_matrix (ctx);
285 g_free (ctx->tmpbuf);
292 audio_convert_get_sizes (AudioConvertCtx * ctx, gint samples, gint * srcsize,
295 g_return_val_if_fail (ctx != NULL, FALSE);
298 *srcsize = samples * ctx->in.unit_size;
300 *dstsize = samples * ctx->out.unit_size;
306 get_temp_buffer (AudioConvertCtx * ctx, gpointer src, gint srcsize,
307 gboolean writable, gint tmpsize)
311 if (srcsize >= tmpsize && writable) {
314 if (ctx->tmpbufsize < tmpsize) {
315 ctx->tmpbuf = g_realloc (ctx->tmpbuf, tmpsize);
316 ctx->tmpbufsize = tmpsize;
318 result = ctx->tmpbuf;
324 audio_convert_convert (AudioConvertCtx * ctx, gpointer src,
325 gpointer dst, gint samples, gboolean src_writable)
331 gboolean bufwritable;
335 g_return_val_if_fail (ctx != NULL, FALSE);
336 g_return_val_if_fail (src != NULL, FALSE);
337 g_return_val_if_fail (dst != NULL, FALSE);
338 g_return_val_if_fail (samples >= 0, FALSE);
343 insize = ctx->in.unit_size * samples;
344 tmpsize = insize * 32 / ctx->in.width;
346 /* this is our source data, we start with the input src data. */
349 bufwritable = src_writable;
351 if (!ctx->in_default) {
352 /* check if final conversion */
353 final = (ctx->out_default && ctx->mix_passthrough);
358 tmpdst = get_temp_buffer (ctx, buf, bufsize, bufwritable, tmpsize);
360 ctx->unpack (buf, tmpdst, ctx->scale, samples * ctx->in.channels);
363 /* new source data, it is writable */
370 if (!ctx->mix_passthrough) {
371 /* see if we need an intermediate step */
372 final = ctx->out_default;
377 tmpdst = get_temp_buffer (ctx, buf, bufsize, bufwritable, tmpsize);
380 gst_channel_mix_mix (ctx, buf, tmpdst, samples);
387 if (!ctx->out_default) {
388 /* output always to dst buffer */
389 ctx->pack (buf, dst, ctx->depth, samples * ctx->out.channels);