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 /* sign bit in the intermediate format */
35 #define SIGNED (1<<31)
40 #define MAKE_UNPACK_FUNC_NAME(name) \
41 audio_convert_unpack_##name
43 #define MAKE_UNPACK_FUNC(name, stride, sign, READ_FUNC) \
45 MAKE_UNPACK_FUNC_NAME (name) (gpointer src, gint32 *dst, \
46 gint scale, gint count) \
48 guint8* p = (guint8 *) src; \
49 for (;count; count--) { \
50 *dst++ = (((gint32) READ_FUNC (p)) << scale) ^ (sign); \
55 /* special unpack code for float */
57 MAKE_UNPACK_FUNC_NAME (float) (gpointer src, gint32 * dst,
58 gint scale, gint count)
60 gfloat *p = (gfloat *) src;
63 for (; count; count--) {
64 temp = *p++ * 2147483647.0f + .5;
65 *dst++ = (gint32) CLAMP (temp, G_MININT, G_MAXINT);
69 #define READ8(p) GST_READ_UINT8(p)
70 #define READ16_FROM_LE(p) GST_READ_UINT16_LE (p)
71 #define READ16_FROM_BE(p) GST_READ_UINT16_BE (p)
72 #define READ24_FROM_LE(p) (p[0] | (p[1] << 8) | (p[2] << 16))
73 #define READ24_FROM_BE(p) (p[2] | (p[1] << 8) | (p[0] << 16))
74 #define READ32_FROM_LE(p) GST_READ_UINT32_LE (p)
75 #define READ32_FROM_BE(p) GST_READ_UINT32_BE (p)
77 MAKE_UNPACK_FUNC (u8, 1, SIGNED, READ8);
78 MAKE_UNPACK_FUNC (s8, 1, 0, READ8);
79 MAKE_UNPACK_FUNC (u16_le, 2, SIGNED, READ16_FROM_LE);
80 MAKE_UNPACK_FUNC (s16_le, 2, 0, READ16_FROM_LE);
81 MAKE_UNPACK_FUNC (u16_be, 2, SIGNED, READ16_FROM_BE);
82 MAKE_UNPACK_FUNC (s16_be, 2, 0, READ16_FROM_BE);
83 MAKE_UNPACK_FUNC (u24_le, 3, SIGNED, READ24_FROM_LE);
84 MAKE_UNPACK_FUNC (s24_le, 3, 0, READ24_FROM_LE);
85 MAKE_UNPACK_FUNC (u24_be, 3, SIGNED, READ24_FROM_BE);
86 MAKE_UNPACK_FUNC (s24_be, 3, 0, READ24_FROM_BE);
87 MAKE_UNPACK_FUNC (u32_le, 4, SIGNED, READ32_FROM_LE);
88 MAKE_UNPACK_FUNC (s32_le, 4, 0, READ32_FROM_LE);
89 MAKE_UNPACK_FUNC (u32_be, 4, SIGNED, READ32_FROM_BE);
90 MAKE_UNPACK_FUNC (s32_be, 4, 0, READ32_FROM_BE);
95 #define MAKE_PACK_FUNC_NAME(name) \
96 audio_convert_pack_##name
98 #define MAKE_PACK_FUNC(name, stride, sign, WRITE_FUNC) \
100 MAKE_PACK_FUNC_NAME (name) (gint32 *src, gpointer dst, \
101 gint scale, gint count) \
103 guint8 *p = (guint8 *)dst; \
105 for (;count; count--) { \
106 tmp = (*src++ ^ (sign)) >> scale; \
107 WRITE_FUNC (p, tmp); \
112 /* special float pack function */
114 MAKE_PACK_FUNC_NAME (float) (gint32 * src, gpointer dst, gint scale, gint count)
116 gfloat *p = (gfloat *) dst;
118 for (; count; count--) {
119 *p++ = INT2FLOAT (*src++);
123 #define WRITE8(p, v) GST_WRITE_UINT8 (p, v)
124 #define WRITE16_TO_LE(p,v) GST_WRITE_UINT16_LE (p, (guint16)(v))
125 #define WRITE16_TO_BE(p,v) GST_WRITE_UINT16_BE (p, (guint16)(v))
126 #define WRITE24_TO_LE(p,v) p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff
127 #define WRITE24_TO_BE(p,v) p[2] = v & 0xff; p[1] = (v >> 8) & 0xff; p[0] = (v >> 16) & 0xff
128 #define WRITE32_TO_LE(p,v) GST_WRITE_UINT32_LE (p, (guint32)(v))
129 #define WRITE32_TO_BE(p,v) GST_WRITE_UINT32_BE (p, (guint32)(v))
131 MAKE_PACK_FUNC (u8, 1, SIGNED, WRITE8);
132 MAKE_PACK_FUNC (s8, 1, 0, WRITE8);
133 MAKE_PACK_FUNC (u16_le, 2, SIGNED, WRITE16_TO_LE);
134 MAKE_PACK_FUNC (s16_le, 2, 0, WRITE16_TO_LE);
135 MAKE_PACK_FUNC (u16_be, 2, SIGNED, WRITE16_TO_BE);
136 MAKE_PACK_FUNC (s16_be, 2, 0, WRITE16_TO_BE);
137 MAKE_PACK_FUNC (u24_le, 3, SIGNED, WRITE24_TO_LE);
138 MAKE_PACK_FUNC (s24_le, 3, 0, WRITE24_TO_LE);
139 MAKE_PACK_FUNC (u24_be, 3, SIGNED, WRITE24_TO_BE);
140 MAKE_PACK_FUNC (s24_be, 3, 0, WRITE24_TO_BE);
141 MAKE_PACK_FUNC (u32_le, 4, SIGNED, WRITE32_TO_LE);
142 MAKE_PACK_FUNC (s32_le, 4, 0, WRITE32_TO_LE);
143 MAKE_PACK_FUNC (u32_be, 4, SIGNED, WRITE32_TO_BE);
144 MAKE_PACK_FUNC (s32_be, 4, 0, WRITE32_TO_BE);
146 static AudioConvertUnpack unpack_funcs[] = {
147 MAKE_UNPACK_FUNC_NAME (u8),
148 MAKE_UNPACK_FUNC_NAME (s8),
149 MAKE_UNPACK_FUNC_NAME (u8),
150 MAKE_UNPACK_FUNC_NAME (s8),
151 MAKE_UNPACK_FUNC_NAME (u16_le),
152 MAKE_UNPACK_FUNC_NAME (s16_le),
153 MAKE_UNPACK_FUNC_NAME (u16_be),
154 MAKE_UNPACK_FUNC_NAME (s16_be),
155 MAKE_UNPACK_FUNC_NAME (u24_le),
156 MAKE_UNPACK_FUNC_NAME (s24_le),
157 MAKE_UNPACK_FUNC_NAME (u24_be),
158 MAKE_UNPACK_FUNC_NAME (s24_be),
159 MAKE_UNPACK_FUNC_NAME (u32_le),
160 MAKE_UNPACK_FUNC_NAME (s32_le),
161 MAKE_UNPACK_FUNC_NAME (u32_be),
162 MAKE_UNPACK_FUNC_NAME (s32_be),
163 MAKE_UNPACK_FUNC_NAME (float),
166 static AudioConvertPack pack_funcs[] = {
167 MAKE_PACK_FUNC_NAME (u8),
168 MAKE_PACK_FUNC_NAME (s8),
169 MAKE_PACK_FUNC_NAME (u8),
170 MAKE_PACK_FUNC_NAME (s8),
171 MAKE_PACK_FUNC_NAME (u16_le),
172 MAKE_PACK_FUNC_NAME (s16_le),
173 MAKE_PACK_FUNC_NAME (u16_be),
174 MAKE_PACK_FUNC_NAME (s16_be),
175 MAKE_PACK_FUNC_NAME (u24_le),
176 MAKE_PACK_FUNC_NAME (s24_le),
177 MAKE_PACK_FUNC_NAME (u24_be),
178 MAKE_PACK_FUNC_NAME (s24_be),
179 MAKE_PACK_FUNC_NAME (u32_le),
180 MAKE_PACK_FUNC_NAME (s32_le),
181 MAKE_PACK_FUNC_NAME (u32_be),
182 MAKE_PACK_FUNC_NAME (s32_be),
183 MAKE_PACK_FUNC_NAME (float),
187 audio_convert_get_func_index (AudioConvertFmt * fmt)
192 index += (fmt->width / 8 - 1) * 4;
193 index += fmt->endianness == G_LITTLE_ENDIAN ? 0 : 2;
194 index += fmt->sign ? 1 : 0;
202 check_default (AudioConvertFmt * fmt)
204 return (fmt->width == 32 && fmt->depth == 32 &&
205 fmt->endianness == G_BYTE_ORDER && fmt->sign == TRUE);
209 audio_convert_clean_fmt (AudioConvertFmt * fmt)
211 g_return_val_if_fail (fmt != NULL, FALSE);
221 audio_convert_prepare_context (AudioConvertCtx * ctx, AudioConvertFmt * in,
222 AudioConvertFmt * out)
226 g_return_val_if_fail (ctx != NULL, FALSE);
227 g_return_val_if_fail (in != NULL, FALSE);
228 g_return_val_if_fail (out != NULL, FALSE);
230 /* first clean the existing context */
231 audio_convert_clean_context (ctx);
236 gst_channel_mix_setup_matrix (ctx);
238 idx = audio_convert_get_func_index (in);
239 if (!(ctx->unpack = unpack_funcs[idx]))
242 idx = audio_convert_get_func_index (out);
243 if (!(ctx->pack = pack_funcs[idx]))
246 /* check if input is in default format */
247 ctx->in_default = check_default (in);
248 /* check if channel mixer is passthrough */
249 ctx->mix_passthrough = gst_channel_mix_passthrough (ctx);
250 /* check if output is in default format */
251 ctx->out_default = check_default (out);
253 ctx->in_scale = 32 - in->depth;
254 ctx->out_scale = 32 - out->depth;
265 audio_convert_clean_context (AudioConvertCtx * ctx)
267 g_return_val_if_fail (ctx != NULL, FALSE);
269 audio_convert_clean_fmt (&ctx->in);
270 audio_convert_clean_fmt (&ctx->out);
271 gst_channel_mix_unset_matrix (ctx);
273 g_free (ctx->tmpbuf);
281 audio_convert_get_sizes (AudioConvertCtx * ctx, gint samples, gint * srcsize,
284 g_return_val_if_fail (ctx != NULL, FALSE);
287 *srcsize = samples * ctx->in.unit_size;
289 *dstsize = samples * ctx->out.unit_size;
295 audio_convert_convert (AudioConvertCtx * ctx, gpointer src,
296 gpointer dst, gint samples, gboolean src_writable)
298 gint insize, outsize;
299 gpointer outbuf, tmpbuf;
302 g_return_val_if_fail (ctx != NULL, FALSE);
303 g_return_val_if_fail (src != NULL, FALSE);
304 g_return_val_if_fail (dst != NULL, FALSE);
305 g_return_val_if_fail (samples >= 0, FALSE);
310 insize = ctx->in.unit_size * samples;
311 outsize = ctx->out.unit_size * samples;
313 /* find biggest temp buffer size */
314 if (!ctx->in_default)
315 biggest = insize * 32 / ctx->in.width;
316 if (!ctx->mix_passthrough)
317 biggest = MAX (biggest, outsize * 32 / ctx->out.width);
319 /* see if one of the buffers can be used as temp */
320 if (outsize >= biggest)
322 else if (insize >= biggest && src_writable)
325 if (biggest > ctx->tmpbufsize) {
326 ctx->tmpbuf = g_realloc (ctx->tmpbuf, biggest);
327 ctx->tmpbufsize = biggest;
329 tmpbuf = ctx->tmpbuf;
332 /* start conversion */
333 if (!ctx->in_default) {
334 /* check if final conversion */
335 if (!(ctx->out_default && ctx->mix_passthrough))
340 /* unpack to default format */
341 ctx->unpack (src, outbuf, ctx->in_scale, samples * ctx->in.channels);
346 if (!ctx->mix_passthrough) {
347 /* check if final conversion */
348 if (!ctx->out_default)
353 /* convert channels */
354 gst_channel_mix_mix (ctx, src, outbuf, samples);
359 if (!ctx->out_default) {
360 /* pack default format into dst */
361 ctx->pack (src, dst, ctx->out_scale, samples * ctx->out.channels);