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.
29 #include "gstchannelmix.h"
30 #include "gstaudioquantize.h"
31 #include "audioconvert.h"
32 #include "gst/floatcast/floatcast.h"
33 #include "gstaudioconvertorc.h"
35 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
36 #define orc_audio_convert_unpack_u16_le orc_audio_convert_unpack_u16
37 #define orc_audio_convert_unpack_u16_be orc_audio_convert_unpack_u16_swap
38 #define orc_audio_convert_unpack_s16_le orc_audio_convert_unpack_s16
39 #define orc_audio_convert_unpack_s16_be orc_audio_convert_unpack_s16_swap
40 #define orc_audio_convert_unpack_u32_le orc_audio_convert_unpack_u32
41 #define orc_audio_convert_unpack_u32_be orc_audio_convert_unpack_u32_swap
42 #define orc_audio_convert_unpack_s32_le orc_audio_convert_unpack_s32
43 #define orc_audio_convert_unpack_s32_be orc_audio_convert_unpack_s32_swap
44 #define orc_audio_convert_unpack_float_le orc_audio_convert_unpack_float_s32
45 #define orc_audio_convert_unpack_float_be orc_audio_convert_unpack_float_s32_swap
46 #define orc_audio_convert_unpack_double_le orc_audio_convert_unpack_double_s32
47 #define orc_audio_convert_unpack_double_be orc_audio_convert_unpack_double_s32_swap
48 #define orc_audio_convert_unpack_float_hq_le orc_audio_convert_unpack_float_double
49 #define orc_audio_convert_unpack_float_hq_be orc_audio_convert_unpack_float_double_swap
50 #define orc_audio_convert_unpack_double_hq_le orc_audio_convert_unpack_double_double
51 #define orc_audio_convert_unpack_double_hq_be orc_audio_convert_unpack_double_double_swap
52 #define orc_audio_convert_unpack_u8_float orc_audio_convert_unpack_u8_double
53 #define orc_audio_convert_unpack_u16_le_float orc_audio_convert_unpack_u16_double
54 #define orc_audio_convert_unpack_u16_be_float orc_audio_convert_unpack_u16_double_swap
55 #define orc_audio_convert_unpack_u32_le_float orc_audio_convert_unpack_u32_double
56 #define orc_audio_convert_unpack_u32_be_float orc_audio_convert_unpack_u32_double_swap
57 #define orc_audio_convert_unpack_s8_float orc_audio_convert_unpack_s8_double
58 #define orc_audio_convert_unpack_s16_le_float orc_audio_convert_unpack_s16_double
59 #define orc_audio_convert_unpack_s16_be_float orc_audio_convert_unpack_s16_double_swap
60 #define orc_audio_convert_unpack_s32_le_float orc_audio_convert_unpack_s32_double
61 #define orc_audio_convert_unpack_s32_be_float orc_audio_convert_unpack_s32_double_swap
62 #define orc_audio_convert_pack_u8 orc_audio_convert_pack_u8
63 #define orc_audio_convert_pack_u16_le orc_audio_convert_pack_u16
64 #define orc_audio_convert_pack_u16_be orc_audio_convert_pack_u16_swap
65 #define orc_audio_convert_pack_s16_le orc_audio_convert_pack_s16
66 #define orc_audio_convert_pack_s16_be orc_audio_convert_pack_s16_swap
67 #define orc_audio_convert_pack_u32_le orc_audio_convert_pack_u32
68 #define orc_audio_convert_pack_u32_be orc_audio_convert_pack_u32_swap
69 #define orc_audio_convert_pack_s32_le orc_audio_convert_pack_s32
70 #define orc_audio_convert_pack_s32_be orc_audio_convert_pack_s32_swap
71 #define orc_audio_convert_pack_float_le orc_audio_convert_pack_s32_float
72 #define orc_audio_convert_pack_float_be orc_audio_convert_pack_s32_float_swap
73 #define orc_audio_convert_pack_double_le orc_audio_convert_pack_s32_double
74 #define orc_audio_convert_pack_double_be orc_audio_convert_pack_s32_double_swap
75 #define orc_audio_convert_pack_float_hq_le orc_audio_convert_pack_double_float
76 #define orc_audio_convert_pack_float_hq_be orc_audio_convert_pack_double_float_swap
77 #define orc_audio_convert_pack_s8_float orc_audio_convert_pack_double_s8
78 #define orc_audio_convert_pack_s16_le_float orc_audio_convert_pack_double_s16
79 #define orc_audio_convert_pack_s16_be_float orc_audio_convert_pack_double_s16_swap
80 #define orc_audio_convert_pack_s32_le_float orc_audio_convert_pack_double_s32
81 #define orc_audio_convert_pack_s32_be_float orc_audio_convert_pack_double_s32_swap
82 #define orc_audio_convert_pack_u8_float orc_audio_convert_pack_double_u8
83 #define orc_audio_convert_pack_u16_le_float orc_audio_convert_pack_double_u16
84 #define orc_audio_convert_pack_u16_be_float orc_audio_convert_pack_double_u16_swap
85 #define orc_audio_convert_pack_u32_le_float orc_audio_convert_pack_double_u32
86 #define orc_audio_convert_pack_u32_be_float orc_audio_convert_pack_double_u32_swap
88 #define orc_audio_convert_unpack_u16_be orc_audio_convert_unpack_u16
89 #define orc_audio_convert_unpack_u16_le orc_audio_convert_unpack_u16_swap
90 #define orc_audio_convert_unpack_s16_be orc_audio_convert_unpack_s16
91 #define orc_audio_convert_unpack_s16_le orc_audio_convert_unpack_s16_swap
92 #define orc_audio_convert_unpack_u32_be orc_audio_convert_unpack_u32
93 #define orc_audio_convert_unpack_u32_le orc_audio_convert_unpack_u32_swap
94 #define orc_audio_convert_unpack_s32_be orc_audio_convert_unpack_s32
95 #define orc_audio_convert_unpack_s32_le orc_audio_convert_unpack_s32_swap
96 #define orc_audio_convert_unpack_float_be orc_audio_convert_unpack_float_s32
97 #define orc_audio_convert_unpack_float_le orc_audio_convert_unpack_float_s32_swap
98 #define orc_audio_convert_unpack_double_be orc_audio_convert_unpack_double_s32
99 #define orc_audio_convert_unpack_double_le orc_audio_convert_unpack_double_s32_swap
100 #define orc_audio_convert_unpack_float_hq_be orc_audio_convert_unpack_float_double
101 #define orc_audio_convert_unpack_float_hq_le orc_audio_convert_unpack_float_double_swap
102 #define orc_audio_convert_unpack_double_hq_be orc_audio_convert_unpack_double_double
103 #define orc_audio_convert_unpack_double_hq_le orc_audio_convert_unpack_double_double_swap
104 #define orc_audio_convert_unpack_u8_float orc_audio_convert_unpack_u8_double
105 #define orc_audio_convert_unpack_u16_be_float orc_audio_convert_unpack_u16_double
106 #define orc_audio_convert_unpack_u16_le_float orc_audio_convert_unpack_u16_double_swap
107 #define orc_audio_convert_unpack_u32_be_float orc_audio_convert_unpack_u32_double
108 #define orc_audio_convert_unpack_u32_le_float orc_audio_convert_unpack_u32_double_swap
109 #define orc_audio_convert_unpack_s8_float orc_audio_convert_unpack_s8_double
110 #define orc_audio_convert_unpack_s16_be_float orc_audio_convert_unpack_s16_double
111 #define orc_audio_convert_unpack_s16_le_float orc_audio_convert_unpack_s16_double_swap
112 #define orc_audio_convert_unpack_s32_be_float orc_audio_convert_unpack_s32_double
113 #define orc_audio_convert_unpack_s32_le_float orc_audio_convert_unpack_s32_double_swap
114 #define orc_audio_convert_pack_u8 orc_audio_convert_pack_u8
115 #define orc_audio_convert_pack_u16_be orc_audio_convert_pack_u16
116 #define orc_audio_convert_pack_u16_le orc_audio_convert_pack_u16_swap
117 #define orc_audio_convert_pack_s16_be orc_audio_convert_pack_s16
118 #define orc_audio_convert_pack_s16_le orc_audio_convert_pack_s16_swap
119 #define orc_audio_convert_pack_u32_be orc_audio_convert_pack_u32
120 #define orc_audio_convert_pack_u32_le orc_audio_convert_pack_u32_swap
121 #define orc_audio_convert_pack_s32_be orc_audio_convert_pack_s32
122 #define orc_audio_convert_pack_s32_le orc_audio_convert_pack_s32_swap
123 #define orc_audio_convert_pack_float_be orc_audio_convert_pack_s32_float
124 #define orc_audio_convert_pack_float_le orc_audio_convert_pack_s32_float_swap
125 #define orc_audio_convert_pack_double_be orc_audio_convert_pack_s32_double
126 #define orc_audio_convert_pack_double_le orc_audio_convert_pack_s32_double_swap
127 #define orc_audio_convert_pack_float_hq_be orc_audio_convert_pack_double_float
128 #define orc_audio_convert_pack_float_hq_le orc_audio_convert_pack_double_float_swap
129 #define orc_audio_convert_pack_s8_float orc_audio_convert_pack_double_s8
130 #define orc_audio_convert_pack_s16_be_float orc_audio_convert_pack_double_s16
131 #define orc_audio_convert_pack_s16_le_float orc_audio_convert_pack_double_s16_swap
132 #define orc_audio_convert_pack_s32_be_float orc_audio_convert_pack_double_s32
133 #define orc_audio_convert_pack_s32_le_float orc_audio_convert_pack_double_s32_swap
134 #define orc_audio_convert_pack_u8_float orc_audio_convert_pack_double_u8
135 #define orc_audio_convert_pack_u16_be_float orc_audio_convert_pack_double_u16
136 #define orc_audio_convert_pack_u16_le_float orc_audio_convert_pack_double_u16_swap
137 #define orc_audio_convert_pack_u32_be_float orc_audio_convert_pack_double_u32
138 #define orc_audio_convert_pack_u32_le_float orc_audio_convert_pack_double_u32_swap
141 /* sign bit in the intermediate format */
142 #define SIGNED (1U<<31)
147 #define MAKE_UNPACK_FUNC_NAME(name) \
148 audio_convert_unpack_##name
149 #define MAKE_ORC_UNPACK_FUNC_NAME(name) \
150 orc_audio_convert_unpack_##name
152 /* unpack from integer to signed integer 32 */
153 #define MAKE_UNPACK_FUNC_II(name, stride, sign, READ_FUNC) \
155 MAKE_UNPACK_FUNC_NAME (name) (guint8 *src, gint32 *dst, \
156 gint scale, gint count) \
158 for (;count; count--) { \
159 *dst++ = (((gint32) READ_FUNC (src)) << scale) ^ (sign); \
164 /* unpack from integer to signed integer 32 with orc */
165 #define MAKE_UNPACK_FUNC_ORC_II(name, stride, sign, READ_FUNC) \
167 MAKE_UNPACK_FUNC_NAME (name) (guint8 *src, gint32 *dst, \
168 gint scale, gint count) \
170 MAKE_ORC_UNPACK_FUNC_NAME (name) (dst, src, scale, count); \
173 /* unpack from float to signed integer 32 */
174 #define MAKE_UNPACK_FUNC_FI(name, type, READ_FUNC) \
176 MAKE_UNPACK_FUNC_NAME (name) (type * src, gint32 * dst, gint s, gint count) \
180 for (; count; count--) { \
181 /* blow up to 32 bit */ \
182 temp = floor ((READ_FUNC (*src++) * 2147483647.0) + 0.5); \
183 *dst++ = (gint32) CLAMP (temp, G_MININT32, G_MAXINT32); \
187 /* unpack from float to signed integer 32 with orc */
188 #define MAKE_UNPACK_FUNC_ORC_FI(name, type, READ_FUNC) \
190 MAKE_UNPACK_FUNC_NAME (name) (type * src, gint32 * dst, gint s, gint count) \
192 MAKE_ORC_UNPACK_FUNC_NAME (name) ((guint32 *) dst, src, count); \
195 /* unpack from float to float 64 (double) */
196 #define MAKE_UNPACK_FUNC_FF(name, type, FUNC) \
198 MAKE_UNPACK_FUNC_NAME (name) (type * src, gdouble * dst, gint s, \
201 for (; count; count--) \
202 *dst++ = (gdouble) FUNC (*src++); \
205 /* unpack from float to float 64 (double) with orc */
206 #define MAKE_UNPACK_FUNC_ORC_FF(name, type, FUNC) \
208 MAKE_UNPACK_FUNC_NAME (name) (type * src, gdouble * dst, gint s, \
211 MAKE_ORC_UNPACK_FUNC_NAME (name) ((gdouble *) dst, src, count); \
214 /* unpack from int to float 64 (double) */
215 #define MAKE_UNPACK_FUNC_IF(name, stride, sign, READ_FUNC) \
217 MAKE_UNPACK_FUNC_NAME (name) (guint8 * src, gdouble * dst, gint scale, \
221 for (; count; count--) { \
222 tmp = (gdouble) ((((gint32) READ_FUNC (src)) << scale) ^ (sign)); \
223 *dst++ = tmp * (1.0 / 2147483647.0); \
228 #define MAKE_UNPACK_FUNC_ORC_IF(name, stride, sign, READ_FUNC) \
230 MAKE_UNPACK_FUNC_NAME (name) (guint8 *src, gdouble *dst, \
231 gint scale, gint count) \
233 MAKE_ORC_UNPACK_FUNC_NAME (name) (dst, src, scale, count); \
236 #define READ8(p) GST_READ_UINT8(p)
237 #define READ16_FROM_LE(p) GST_READ_UINT16_LE (p)
238 #define READ16_FROM_BE(p) GST_READ_UINT16_BE (p)
239 #define READ24_FROM_LE(p) (p[0] | (p[1] << 8) | (p[2] << 16))
240 #define READ24_FROM_BE(p) (p[2] | (p[1] << 8) | (p[0] << 16))
241 #define READ32_FROM_LE(p) GST_READ_UINT32_LE (p)
242 #define READ32_FROM_BE(p) GST_READ_UINT32_BE (p)
246 MAKE_UNPACK_FUNC_ORC_II (u8, 1, SIGNED, READ8);
247 MAKE_UNPACK_FUNC_ORC_II (s8, 1, 0, READ8);
248 MAKE_UNPACK_FUNC_ORC_II (u16_le, 2, SIGNED, READ16_FROM_LE);
249 MAKE_UNPACK_FUNC_ORC_II (s16_le, 2, 0, READ16_FROM_LE);
250 MAKE_UNPACK_FUNC_ORC_II (u16_be, 2, SIGNED, READ16_FROM_BE);
251 MAKE_UNPACK_FUNC_ORC_II (s16_be, 2, 0, READ16_FROM_BE);
252 MAKE_UNPACK_FUNC_II (u24_le, 3, SIGNED, READ24_FROM_LE);
253 MAKE_UNPACK_FUNC_II (s24_le, 3, 0, READ24_FROM_LE);
254 MAKE_UNPACK_FUNC_II (u24_be, 3, SIGNED, READ24_FROM_BE);
255 MAKE_UNPACK_FUNC_II (s24_be, 3, 0, READ24_FROM_BE);
256 MAKE_UNPACK_FUNC_ORC_II (u32_le, 4, SIGNED, READ32_FROM_LE);
257 MAKE_UNPACK_FUNC_ORC_II (s32_le, 4, 0, READ32_FROM_LE);
258 MAKE_UNPACK_FUNC_ORC_II (u32_be, 4, SIGNED, READ32_FROM_BE);
259 MAKE_UNPACK_FUNC_ORC_II (s32_be, 4, 0, READ32_FROM_BE);
260 MAKE_UNPACK_FUNC_ORC_FI (float_le, gfloat, GFLOAT_FROM_LE);
261 MAKE_UNPACK_FUNC_ORC_FI (float_be, gfloat, GFLOAT_FROM_BE);
262 MAKE_UNPACK_FUNC_ORC_FI (double_le, gdouble, GDOUBLE_FROM_LE);
263 MAKE_UNPACK_FUNC_ORC_FI (double_be, gdouble, GDOUBLE_FROM_BE);
264 MAKE_UNPACK_FUNC_ORC_FF (float_hq_le, gfloat, GFLOAT_FROM_LE);
265 MAKE_UNPACK_FUNC_ORC_FF (float_hq_be, gfloat, GFLOAT_FROM_BE);
266 MAKE_UNPACK_FUNC_ORC_FF (double_hq_le, gdouble, GDOUBLE_FROM_LE);
267 MAKE_UNPACK_FUNC_ORC_FF (double_hq_be, gdouble, GDOUBLE_FROM_BE);
268 MAKE_UNPACK_FUNC_ORC_IF (u8_float, 1, SIGNED, READ8);
269 MAKE_UNPACK_FUNC_ORC_IF (s8_float, 1, 0, READ8);
270 MAKE_UNPACK_FUNC_ORC_IF (u16_le_float, 2, SIGNED, READ16_FROM_LE);
271 MAKE_UNPACK_FUNC_ORC_IF (s16_le_float, 2, 0, READ16_FROM_LE);
272 MAKE_UNPACK_FUNC_ORC_IF (u16_be_float, 2, SIGNED, READ16_FROM_BE);
273 MAKE_UNPACK_FUNC_ORC_IF (s16_be_float, 2, 0, READ16_FROM_BE);
274 MAKE_UNPACK_FUNC_IF (u24_le_float, 3, SIGNED, READ24_FROM_LE);
275 MAKE_UNPACK_FUNC_IF (s24_le_float, 3, 0, READ24_FROM_LE);
276 MAKE_UNPACK_FUNC_IF (u24_be_float, 3, SIGNED, READ24_FROM_BE);
277 MAKE_UNPACK_FUNC_IF (s24_be_float, 3, 0, READ24_FROM_BE);
278 MAKE_UNPACK_FUNC_ORC_IF (u32_le_float, 4, SIGNED, READ32_FROM_LE);
279 MAKE_UNPACK_FUNC_ORC_IF (s32_le_float, 4, 0, READ32_FROM_LE);
280 MAKE_UNPACK_FUNC_ORC_IF (u32_be_float, 4, SIGNED, READ32_FROM_BE);
281 MAKE_UNPACK_FUNC_ORC_IF (s32_be_float, 4, 0, READ32_FROM_BE);
283 /* One of the double_hq_* functions generated above is ineffecient, but it's
284 * never used anyway. The same is true for one of the s32_* functions. */
289 #define MAKE_PACK_FUNC_NAME(name) \
290 audio_convert_pack_##name
291 #define MAKE_PACK_FUNC_NAME_ORC(name) \
292 orc_audio_convert_pack_##name
295 * These functions convert the signed 32 bit integers to the
296 * target format. For this to work the following steps are done:
298 * 1) If the output format is unsigned we will XOR the sign bit. This
299 * will do the same as if we add 1<<31.
300 * 2) Afterwards we shift to the target depth. It's necessary to left-shift
301 * on signed values here to get arithmetical shifting.
302 * 3) This is then written into our target array by the corresponding write
303 * function for the target width.
306 /* pack from signed integer 32 to integer using Orc */
307 #define MAKE_PACK_FUNC_ORC_II(name, stride, sign, WRITE_FUNC) \
309 MAKE_PACK_FUNC_NAME (name) (gint32 *src, guint8 * dst, \
310 gint scale, gint count) \
312 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, scale, count); \
315 /* pack from signed integer 32 to integer */
316 #define MAKE_PACK_FUNC_II(name, stride, sign, WRITE_FUNC) \
318 MAKE_PACK_FUNC_NAME (name) (gint32 *src, guint8 * dst, \
319 gint scale, gint count) \
322 for (;count; count--) { \
323 tmp = (*src++ ^ (sign)) >> scale; \
324 WRITE_FUNC (dst, tmp); \
329 /* pack from signed integer 32 to float using orc */
330 #define MAKE_PACK_FUNC_ORC_IF(name, type, FUNC) \
332 MAKE_PACK_FUNC_NAME (name) (gint32 * src, type * dst, gint scale, \
335 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, count); \
338 /* pack from signed integer 32 to float */
339 #define MAKE_PACK_FUNC_IF(name, type, FUNC) \
341 MAKE_PACK_FUNC_NAME (name) (gint32 * src, type * dst, gint scale, \
344 for (; count; count--) \
345 *dst++ = FUNC ((type) ((*src++) * (1.0 / 2147483647.0))); \
348 /* pack from float 64 (double) to float */
349 #define MAKE_PACK_FUNC_FF(name, type, FUNC) \
351 MAKE_PACK_FUNC_NAME (name) (gdouble * src, type * dst, gint s, \
354 for (; count; count--) \
355 *dst++ = FUNC ((type) (*src++)); \
358 /* pack from float 64 (double) to float with orc */
359 #define MAKE_PACK_FUNC_ORC_FF(name, type, FUNC) \
361 MAKE_PACK_FUNC_NAME (name) (gdouble * src, type * dst, gint s, \
364 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, count); \
367 /* pack from float 64 (double) to signed int.
368 * the floats are already in the correct range. Only a cast is needed.
370 #define MAKE_PACK_FUNC_FI_S(name, stride, WRITE_FUNC) \
372 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
376 for (; count; count--) { \
377 tmp = (gint32) (*src); \
378 WRITE_FUNC (dst, tmp); \
384 /* pack from float 64 (double) to unsigned int.
385 * the floats are already in the correct range. Only a cast is needed
386 * and an addition of 2^(target_depth-1) to get in the correct unsigned
388 #define MAKE_PACK_FUNC_FI_U(name, stride, WRITE_FUNC) \
390 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
394 gdouble limit = (1U<<(32-scale-1)); \
395 for (; count; count--) { \
396 tmp = (guint32) (*src + limit); \
397 WRITE_FUNC (dst, tmp); \
403 /* pack from float 64 (double) to signed int with orc.
404 * the floats are already in the correct range. Only a cast is needed.
406 #define MAKE_PACK_FUNC_ORC_FI_S(name, stride, WRITE_FUNC) \
408 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
411 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, scale, count); \
414 /* pack from float 64 (double) to unsigned int with orc.
415 * the floats are already in the correct range. Only a cast is needed.
417 #define MAKE_PACK_FUNC_ORC_FI_U(name, stride, WRITE_FUNC) \
419 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
422 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, scale, count); \
425 #define WRITE8(p, v) GST_WRITE_UINT8 (p, v)
426 #define WRITE16_TO_LE(p,v) GST_WRITE_UINT16_LE (p, (guint16)(v))
427 #define WRITE16_TO_BE(p,v) GST_WRITE_UINT16_BE (p, (guint16)(v))
428 #define WRITE24_TO_LE(p,v) p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff
429 #define WRITE24_TO_BE(p,v) p[2] = v & 0xff; p[1] = (v >> 8) & 0xff; p[0] = (v >> 16) & 0xff
430 #define WRITE32_TO_LE(p,v) GST_WRITE_UINT32_LE (p, (guint32)(v))
431 #define WRITE32_TO_BE(p,v) GST_WRITE_UINT32_BE (p, (guint32)(v))
433 MAKE_PACK_FUNC_ORC_II (u8, 1, SIGNED, WRITE8);
434 MAKE_PACK_FUNC_ORC_II (s8, 1, 0, WRITE8);
435 MAKE_PACK_FUNC_ORC_II (u16_le, 2, SIGNED, WRITE16_TO_LE);
436 MAKE_PACK_FUNC_ORC_II (s16_le, 2, 0, WRITE16_TO_LE);
437 MAKE_PACK_FUNC_ORC_II (u16_be, 2, SIGNED, WRITE16_TO_BE);
438 MAKE_PACK_FUNC_ORC_II (s16_be, 2, 0, WRITE16_TO_BE);
439 MAKE_PACK_FUNC_II (u24_le, 3, SIGNED, WRITE24_TO_LE);
440 MAKE_PACK_FUNC_II (s24_le, 3, 0, WRITE24_TO_LE);
441 MAKE_PACK_FUNC_II (u24_be, 3, SIGNED, WRITE24_TO_BE);
442 MAKE_PACK_FUNC_II (s24_be, 3, 0, WRITE24_TO_BE);
443 MAKE_PACK_FUNC_ORC_II (u32_le, 4, SIGNED, WRITE32_TO_LE);
444 MAKE_PACK_FUNC_ORC_II (s32_le, 4, 0, WRITE32_TO_LE);
445 MAKE_PACK_FUNC_ORC_II (u32_be, 4, SIGNED, WRITE32_TO_BE);
446 MAKE_PACK_FUNC_ORC_II (s32_be, 4, 0, WRITE32_TO_BE);
447 MAKE_PACK_FUNC_ORC_IF (float_le, gfloat, GFLOAT_TO_LE);
448 MAKE_PACK_FUNC_ORC_IF (float_be, gfloat, GFLOAT_TO_BE);
449 MAKE_PACK_FUNC_ORC_IF (double_le, gdouble, GDOUBLE_TO_LE);
450 MAKE_PACK_FUNC_ORC_IF (double_be, gdouble, GDOUBLE_TO_BE);
451 MAKE_PACK_FUNC_ORC_FF (float_hq_le, gfloat, GFLOAT_TO_LE);
452 MAKE_PACK_FUNC_ORC_FF (float_hq_be, gfloat, GFLOAT_TO_BE);
453 MAKE_PACK_FUNC_ORC_FI_U (u8_float, 1, WRITE8);
454 MAKE_PACK_FUNC_ORC_FI_S (s8_float, 1, WRITE8);
455 MAKE_PACK_FUNC_ORC_FI_U (u16_le_float, 2, WRITE16_TO_LE);
456 MAKE_PACK_FUNC_ORC_FI_S (s16_le_float, 2, WRITE16_TO_LE);
457 MAKE_PACK_FUNC_ORC_FI_U (u16_be_float, 2, WRITE16_TO_BE);
458 MAKE_PACK_FUNC_ORC_FI_S (s16_be_float, 2, WRITE16_TO_BE);
459 MAKE_PACK_FUNC_FI_U (u24_le_float, 3, WRITE24_TO_LE);
460 MAKE_PACK_FUNC_FI_S (s24_le_float, 3, WRITE24_TO_LE);
461 MAKE_PACK_FUNC_FI_U (u24_be_float, 3, WRITE24_TO_BE);
462 MAKE_PACK_FUNC_FI_S (s24_be_float, 3, WRITE24_TO_BE);
463 MAKE_PACK_FUNC_ORC_FI_U (u32_le_float, 4, WRITE32_TO_LE);
464 MAKE_PACK_FUNC_ORC_FI_S (s32_le_float, 4, WRITE32_TO_LE);
465 MAKE_PACK_FUNC_ORC_FI_U (u32_be_float, 4, WRITE32_TO_BE);
466 MAKE_PACK_FUNC_ORC_FI_S (s32_be_float, 4, WRITE32_TO_BE);
468 /* For double_hq, packing and unpacking is the same, so we reuse the unpacking
470 #define audio_convert_pack_double_hq_le MAKE_UNPACK_FUNC_NAME (double_hq_le)
471 #define audio_convert_pack_double_hq_be MAKE_UNPACK_FUNC_NAME (double_hq_be)
473 static AudioConvertUnpack unpack_funcs[] = {
474 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8),
475 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8),
476 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8),
477 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8),
478 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_le),
479 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_le),
480 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_be),
481 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_be),
482 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_le),
483 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_le),
484 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_be),
485 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_be),
486 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_le),
487 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_le),
488 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_be),
489 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_be),
490 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_le),
491 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_be),
492 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_le),
493 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_be),
494 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_hq_le),
495 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_hq_be),
496 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_hq_le),
497 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_hq_be),
498 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8_float),
499 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8_float),
500 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8_float),
501 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8_float),
502 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_le_float),
503 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_le_float),
504 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_be_float),
505 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_be_float),
506 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_le_float),
507 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_le_float),
508 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_be_float),
509 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_be_float),
510 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_le_float),
511 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_le_float),
512 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_be_float),
513 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_be_float),
516 static AudioConvertPack pack_funcs[] = {
517 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8),
518 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8),
519 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8),
520 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8),
521 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_le),
522 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_le),
523 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_be),
524 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_be),
525 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_le),
526 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_le),
527 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_be),
528 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_be),
529 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_le),
530 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_le),
531 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_be),
532 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_be),
533 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_le),
534 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_be),
535 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_le),
536 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_be),
537 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_hq_le),
538 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_hq_be),
539 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_hq_le),
540 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_hq_be),
541 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8_float),
542 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8_float),
543 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8_float),
544 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8_float),
545 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_le_float),
546 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_le_float),
547 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_be_float),
548 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_be_float),
549 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_le_float),
550 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_le_float),
551 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_be_float),
552 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_be_float),
553 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_le_float),
554 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_le_float),
555 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_be_float),
556 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_be_float),
559 #define DOUBLE_INTERMEDIATE_FORMAT(ctx) \
560 ((!GST_AUDIO_FORMAT_INFO_IS_INTEGER (ctx->in.finfo) && \
561 !GST_AUDIO_FORMAT_INFO_IS_INTEGER (ctx->out.finfo)) || \
562 (ctx->ns != NOISE_SHAPING_NONE))
565 audio_convert_get_func_index (AudioConvertCtx * ctx,
566 const GstAudioFormatInfo * fmt)
570 if (GST_AUDIO_FORMAT_INFO_IS_INTEGER (fmt)) {
571 index += (GST_AUDIO_FORMAT_INFO_WIDTH (fmt) / 8 - 1) * 4;
572 index += GST_AUDIO_FORMAT_INFO_IS_LE (fmt) ? 0 : 2;
573 index += GST_AUDIO_FORMAT_INFO_IS_SIGNED (fmt) ? 1 : 0;
574 index += (ctx->ns == NOISE_SHAPING_NONE) ? 0 : 24;
576 /* this is float/double */
578 index += (GST_AUDIO_FORMAT_INFO_WIDTH (fmt) == 32) ? 0 : 2;
579 index += GST_AUDIO_FORMAT_INFO_IS_LE (fmt) ? 0 : 1;
580 index += (DOUBLE_INTERMEDIATE_FORMAT (ctx)) ? 4 : 0;
586 static inline gboolean
587 check_default (AudioConvertCtx * ctx, const GstAudioFormatInfo * fmt)
589 if (!DOUBLE_INTERMEDIATE_FORMAT (ctx)) {
590 return GST_AUDIO_FORMAT_INFO_FORMAT (fmt) == GST_AUDIO_FORMAT_S32;
592 return GST_AUDIO_FORMAT_INFO_FORMAT (fmt) == GST_AUDIO_FORMAT_F64;
597 audio_convert_prepare_context (AudioConvertCtx * ctx, GstAudioInfo * in,
598 GstAudioInfo * out, GstAudioConvertDithering dither,
599 GstAudioConvertNoiseShaping ns)
601 gint idx_in, idx_out;
602 gint in_depth, out_depth;
604 g_return_val_if_fail (ctx != NULL, FALSE);
605 g_return_val_if_fail (in != NULL, FALSE);
606 g_return_val_if_fail (out != NULL, FALSE);
608 /* first clean the existing context */
609 audio_convert_clean_context (ctx);
614 in_depth = GST_AUDIO_FORMAT_INFO_DEPTH (in->finfo);
615 out_depth = GST_AUDIO_FORMAT_INFO_DEPTH (out->finfo);
617 /* Don't dither or apply noise shaping if target depth is bigger than 20 bits
618 * as DA converters only can do a SNR up to 20 bits in reality.
619 * Also don't dither or apply noise shaping if target depth is larger than
621 if (out_depth <= 20 && (!GST_AUDIO_FORMAT_INFO_IS_INTEGER (in->finfo)
622 || in_depth >= out_depth)) {
623 ctx->dither = dither;
626 ctx->dither = DITHER_NONE;
627 ctx->ns = NOISE_SHAPING_NONE;
630 /* Use simple error feedback when output sample rate is smaller than
631 * 32000 as the other methods might move the noise to audible ranges */
632 if (ctx->ns > NOISE_SHAPING_ERROR_FEEDBACK && out->rate < 32000)
633 ctx->ns = NOISE_SHAPING_ERROR_FEEDBACK;
635 gst_channel_mix_setup_matrix (ctx);
637 idx_in = audio_convert_get_func_index (ctx, in->finfo);
638 ctx->unpack = unpack_funcs[idx_in];
640 idx_out = audio_convert_get_func_index (ctx, out->finfo);
641 ctx->pack = pack_funcs[idx_out];
643 /* if both formats are float/double or we use noise shaping use double as
644 * intermediate format and and switch mixing */
645 if (!DOUBLE_INTERMEDIATE_FORMAT (ctx)) {
646 GST_INFO ("use int mixing");
647 ctx->channel_mix = (AudioConvertMix) gst_channel_mix_mix_int;
649 GST_INFO ("use float mixing");
650 ctx->channel_mix = (AudioConvertMix) gst_channel_mix_mix_float;
652 GST_INFO ("unitsizes: %d -> %d", in->bpf, out->bpf);
654 /* check if input is in default format */
655 ctx->in_default = check_default (ctx, in->finfo);
656 /* check if channel mixer is passthrough */
657 ctx->mix_passthrough = gst_channel_mix_passthrough (ctx);
658 /* check if output is in default format */
659 ctx->out_default = check_default (ctx, out->finfo);
661 GST_INFO ("in default %d, mix passthrough %d, out default %d",
662 ctx->in_default, ctx->mix_passthrough, ctx->out_default);
665 GST_AUDIO_FORMAT_INFO_IS_INTEGER (in->finfo) ? (32 - in_depth) : 0;
667 GST_AUDIO_FORMAT_INFO_IS_INTEGER (out->finfo) ? (32 - out_depth) : 0;
669 gst_audio_quantize_setup (ctx);
675 audio_convert_clean_context (AudioConvertCtx * ctx)
677 g_return_val_if_fail (ctx != NULL, FALSE);
679 gst_audio_quantize_free (ctx);
680 gst_audio_info_init (&ctx->in);
681 gst_audio_info_init (&ctx->out);
682 gst_channel_mix_unset_matrix (ctx);
684 g_free (ctx->tmpbuf);
692 audio_convert_get_sizes (AudioConvertCtx * ctx, gint samples, gint * srcsize,
695 g_return_val_if_fail (ctx != NULL, FALSE);
698 *srcsize = samples * ctx->in.bpf;
700 *dstsize = samples * ctx->out.bpf;
706 audio_convert_convert (AudioConvertCtx * ctx, gpointer src,
707 gpointer dst, gint samples, gboolean src_writable)
709 guint insize, outsize, size;
710 gpointer outbuf, tmpbuf;
711 guint intemp = 0, outtemp = 0, biggest;
712 gint in_width, out_width;
714 g_return_val_if_fail (ctx != NULL, FALSE);
715 g_return_val_if_fail (src != NULL, FALSE);
716 g_return_val_if_fail (dst != NULL, FALSE);
717 g_return_val_if_fail (samples >= 0, FALSE);
722 insize = ctx->in.bpf * samples;
723 outsize = ctx->out.bpf * samples;
725 in_width = GST_AUDIO_FORMAT_INFO_WIDTH (ctx->in.finfo);
726 out_width = GST_AUDIO_FORMAT_INFO_WIDTH (ctx->out.finfo);
728 /* find biggest temp buffer size */
729 size = (DOUBLE_INTERMEDIATE_FORMAT (ctx)) ? sizeof (gdouble)
732 if (!ctx->in_default)
733 intemp = gst_util_uint64_scale (insize, size * 8, in_width);
734 if (!ctx->mix_passthrough || !ctx->out_default)
735 outtemp = gst_util_uint64_scale (outsize, size * 8, out_width);
736 biggest = MAX (intemp, outtemp);
738 /* see if one of the buffers can be used as temp */
739 if ((outsize >= biggest) && (ctx->out.bpf <= size))
741 else if ((insize >= biggest) && src_writable && (ctx->in.bpf >= size))
744 if (biggest > ctx->tmpbufsize) {
745 ctx->tmpbuf = g_realloc (ctx->tmpbuf, biggest);
746 ctx->tmpbufsize = biggest;
748 tmpbuf = ctx->tmpbuf;
751 /* start conversion */
752 if (!ctx->in_default) {
753 /* check if final conversion */
754 if (!(ctx->out_default && ctx->mix_passthrough))
759 /* unpack to default format */
760 ctx->unpack (src, outbuf, ctx->in_scale, samples * ctx->in.channels);
765 if (!ctx->mix_passthrough) {
766 /* check if final conversion */
767 if (!ctx->out_default)
772 /* convert channels */
773 ctx->channel_mix (ctx, src, outbuf, samples);
778 /* we only need to quantize if output format is int */
779 if (GST_AUDIO_FORMAT_INFO_IS_INTEGER (ctx->out.finfo)) {
780 if (ctx->out_default)
784 ctx->quantize (ctx, src, outbuf, samples);
787 if (!ctx->out_default) {
788 /* pack default format into dst */
789 ctx->pack (src, dst, ctx->out_scale, samples * ctx->out.channels);