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 "gstaudioconvertorc.h"
34 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
35 #define orc_audio_convert_unpack_u16_le orc_audio_convert_unpack_u16
36 #define orc_audio_convert_unpack_u16_be orc_audio_convert_unpack_u16_swap
37 #define orc_audio_convert_unpack_s16_le orc_audio_convert_unpack_s16
38 #define orc_audio_convert_unpack_s16_be orc_audio_convert_unpack_s16_swap
39 #define orc_audio_convert_unpack_u32_le orc_audio_convert_unpack_u32
40 #define orc_audio_convert_unpack_u32_be orc_audio_convert_unpack_u32_swap
41 #define orc_audio_convert_unpack_s32_le orc_audio_convert_unpack_s32
42 #define orc_audio_convert_unpack_s32_be orc_audio_convert_unpack_s32_swap
43 #define orc_audio_convert_unpack_float_le orc_audio_convert_unpack_float_s32
44 #define orc_audio_convert_unpack_float_be orc_audio_convert_unpack_float_s32_swap
45 #define orc_audio_convert_unpack_double_le orc_audio_convert_unpack_double_s32
46 #define orc_audio_convert_unpack_double_be orc_audio_convert_unpack_double_s32_swap
47 #define orc_audio_convert_unpack_float_hq_le orc_audio_convert_unpack_float_double
48 #define orc_audio_convert_unpack_float_hq_be orc_audio_convert_unpack_float_double_swap
49 #define orc_audio_convert_unpack_double_hq_le orc_audio_convert_unpack_double_double
50 #define orc_audio_convert_unpack_double_hq_be orc_audio_convert_unpack_double_double_swap
51 #define orc_audio_convert_unpack_u8_float orc_audio_convert_unpack_u8_double
52 #define orc_audio_convert_unpack_u16_le_float orc_audio_convert_unpack_u16_double
53 #define orc_audio_convert_unpack_u16_be_float orc_audio_convert_unpack_u16_double_swap
54 #define orc_audio_convert_unpack_u32_le_float orc_audio_convert_unpack_u32_double
55 #define orc_audio_convert_unpack_u32_be_float orc_audio_convert_unpack_u32_double_swap
56 #define orc_audio_convert_unpack_s8_float orc_audio_convert_unpack_s8_double
57 #define orc_audio_convert_unpack_s16_le_float orc_audio_convert_unpack_s16_double
58 #define orc_audio_convert_unpack_s16_be_float orc_audio_convert_unpack_s16_double_swap
59 #define orc_audio_convert_unpack_s32_le_float orc_audio_convert_unpack_s32_double
60 #define orc_audio_convert_unpack_s32_be_float orc_audio_convert_unpack_s32_double_swap
61 #define orc_audio_convert_pack_u8 orc_audio_convert_pack_u8
62 #define orc_audio_convert_pack_u16_le orc_audio_convert_pack_u16
63 #define orc_audio_convert_pack_u16_be orc_audio_convert_pack_u16_swap
64 #define orc_audio_convert_pack_s16_le orc_audio_convert_pack_s16
65 #define orc_audio_convert_pack_s16_be orc_audio_convert_pack_s16_swap
66 #define orc_audio_convert_pack_u32_le orc_audio_convert_pack_u32
67 #define orc_audio_convert_pack_u32_be orc_audio_convert_pack_u32_swap
68 #define orc_audio_convert_pack_s32_le orc_audio_convert_pack_s32
69 #define orc_audio_convert_pack_s32_be orc_audio_convert_pack_s32_swap
70 #define orc_audio_convert_pack_float_le orc_audio_convert_pack_s32_float
71 #define orc_audio_convert_pack_float_be orc_audio_convert_pack_s32_float_swap
72 #define orc_audio_convert_pack_double_le orc_audio_convert_pack_s32_double
73 #define orc_audio_convert_pack_double_be orc_audio_convert_pack_s32_double_swap
74 #define orc_audio_convert_pack_float_hq_le orc_audio_convert_pack_double_float
75 #define orc_audio_convert_pack_float_hq_be orc_audio_convert_pack_double_float_swap
76 #define orc_audio_convert_pack_s8_float orc_audio_convert_pack_double_s8
77 #define orc_audio_convert_pack_s16_le_float orc_audio_convert_pack_double_s16
78 #define orc_audio_convert_pack_s16_be_float orc_audio_convert_pack_double_s16_swap
79 #define orc_audio_convert_pack_s32_le_float orc_audio_convert_pack_double_s32
80 #define orc_audio_convert_pack_s32_be_float orc_audio_convert_pack_double_s32_swap
81 #define orc_audio_convert_pack_u8_float orc_audio_convert_pack_double_u8
82 #define orc_audio_convert_pack_u16_le_float orc_audio_convert_pack_double_u16
83 #define orc_audio_convert_pack_u16_be_float orc_audio_convert_pack_double_u16_swap
84 #define orc_audio_convert_pack_u32_le_float orc_audio_convert_pack_double_u32
85 #define orc_audio_convert_pack_u32_be_float orc_audio_convert_pack_double_u32_swap
87 #define orc_audio_convert_unpack_u16_be orc_audio_convert_unpack_u16
88 #define orc_audio_convert_unpack_u16_le orc_audio_convert_unpack_u16_swap
89 #define orc_audio_convert_unpack_s16_be orc_audio_convert_unpack_s16
90 #define orc_audio_convert_unpack_s16_le orc_audio_convert_unpack_s16_swap
91 #define orc_audio_convert_unpack_u32_be orc_audio_convert_unpack_u32
92 #define orc_audio_convert_unpack_u32_le orc_audio_convert_unpack_u32_swap
93 #define orc_audio_convert_unpack_s32_be orc_audio_convert_unpack_s32
94 #define orc_audio_convert_unpack_s32_le orc_audio_convert_unpack_s32_swap
95 #define orc_audio_convert_unpack_float_be orc_audio_convert_unpack_float_s32
96 #define orc_audio_convert_unpack_float_le orc_audio_convert_unpack_float_s32_swap
97 #define orc_audio_convert_unpack_double_be orc_audio_convert_unpack_double_s32
98 #define orc_audio_convert_unpack_double_le orc_audio_convert_unpack_double_s32_swap
99 #define orc_audio_convert_unpack_float_hq_be orc_audio_convert_unpack_float_double
100 #define orc_audio_convert_unpack_float_hq_le orc_audio_convert_unpack_float_double_swap
101 #define orc_audio_convert_unpack_double_hq_be orc_audio_convert_unpack_double_double
102 #define orc_audio_convert_unpack_double_hq_le orc_audio_convert_unpack_double_double_swap
103 #define orc_audio_convert_unpack_u8_float orc_audio_convert_unpack_u8_double
104 #define orc_audio_convert_unpack_u16_be_float orc_audio_convert_unpack_u16_double
105 #define orc_audio_convert_unpack_u16_le_float orc_audio_convert_unpack_u16_double_swap
106 #define orc_audio_convert_unpack_u32_be_float orc_audio_convert_unpack_u32_double
107 #define orc_audio_convert_unpack_u32_le_float orc_audio_convert_unpack_u32_double_swap
108 #define orc_audio_convert_unpack_s8_float orc_audio_convert_unpack_s8_double
109 #define orc_audio_convert_unpack_s16_be_float orc_audio_convert_unpack_s16_double
110 #define orc_audio_convert_unpack_s16_le_float orc_audio_convert_unpack_s16_double_swap
111 #define orc_audio_convert_unpack_s32_be_float orc_audio_convert_unpack_s32_double
112 #define orc_audio_convert_unpack_s32_le_float orc_audio_convert_unpack_s32_double_swap
113 #define orc_audio_convert_pack_u8 orc_audio_convert_pack_u8
114 #define orc_audio_convert_pack_u16_be orc_audio_convert_pack_u16
115 #define orc_audio_convert_pack_u16_le orc_audio_convert_pack_u16_swap
116 #define orc_audio_convert_pack_s16_be orc_audio_convert_pack_s16
117 #define orc_audio_convert_pack_s16_le orc_audio_convert_pack_s16_swap
118 #define orc_audio_convert_pack_u32_be orc_audio_convert_pack_u32
119 #define orc_audio_convert_pack_u32_le orc_audio_convert_pack_u32_swap
120 #define orc_audio_convert_pack_s32_be orc_audio_convert_pack_s32
121 #define orc_audio_convert_pack_s32_le orc_audio_convert_pack_s32_swap
122 #define orc_audio_convert_pack_float_be orc_audio_convert_pack_s32_float
123 #define orc_audio_convert_pack_float_le orc_audio_convert_pack_s32_float_swap
124 #define orc_audio_convert_pack_double_be orc_audio_convert_pack_s32_double
125 #define orc_audio_convert_pack_double_le orc_audio_convert_pack_s32_double_swap
126 #define orc_audio_convert_pack_float_hq_be orc_audio_convert_pack_double_float
127 #define orc_audio_convert_pack_float_hq_le orc_audio_convert_pack_double_float_swap
128 #define orc_audio_convert_pack_s8_float orc_audio_convert_pack_double_s8
129 #define orc_audio_convert_pack_s16_be_float orc_audio_convert_pack_double_s16
130 #define orc_audio_convert_pack_s16_le_float orc_audio_convert_pack_double_s16_swap
131 #define orc_audio_convert_pack_s32_be_float orc_audio_convert_pack_double_s32
132 #define orc_audio_convert_pack_s32_le_float orc_audio_convert_pack_double_s32_swap
133 #define orc_audio_convert_pack_u8_float orc_audio_convert_pack_double_u8
134 #define orc_audio_convert_pack_u16_be_float orc_audio_convert_pack_double_u16
135 #define orc_audio_convert_pack_u16_le_float orc_audio_convert_pack_double_u16_swap
136 #define orc_audio_convert_pack_u32_be_float orc_audio_convert_pack_double_u32
137 #define orc_audio_convert_pack_u32_le_float orc_audio_convert_pack_double_u32_swap
140 /* sign bit in the intermediate format */
141 #define SIGNED (1U<<31)
146 #define MAKE_UNPACK_FUNC_NAME(name) \
147 audio_convert_unpack_##name
148 #define MAKE_ORC_UNPACK_FUNC_NAME(name) \
149 orc_audio_convert_unpack_##name
151 /* unpack from integer to signed integer 32 */
152 #define MAKE_UNPACK_FUNC_II(name, stride, sign, READ_FUNC) \
154 MAKE_UNPACK_FUNC_NAME (name) (guint8 *src, gint32 *dst, \
155 gint scale, gint count) \
157 for (;count; count--) { \
158 *dst++ = (((gint32) READ_FUNC (src)) << scale) ^ (sign); \
163 /* unpack from integer to signed integer 32 with orc */
164 #define MAKE_UNPACK_FUNC_ORC_II(name, stride, sign, READ_FUNC) \
166 MAKE_UNPACK_FUNC_NAME (name) (guint8 *src, gint32 *dst, \
167 gint scale, gint count) \
169 MAKE_ORC_UNPACK_FUNC_NAME (name) (dst, src, scale, count); \
172 /* unpack from float to signed integer 32 */
173 #define MAKE_UNPACK_FUNC_FI(name, type, READ_FUNC) \
175 MAKE_UNPACK_FUNC_NAME (name) (type * src, gint32 * dst, gint s, gint count) \
179 for (; count; count--) { \
180 /* blow up to 32 bit */ \
181 temp = floor ((READ_FUNC (*src++) * 2147483647.0) + 0.5); \
182 *dst++ = (gint32) CLAMP (temp, G_MININT32, G_MAXINT32); \
186 /* unpack from float to signed integer 32 with orc */
187 #define MAKE_UNPACK_FUNC_ORC_FI(name, type, READ_FUNC) \
189 MAKE_UNPACK_FUNC_NAME (name) (type * src, gint32 * dst, gint s, gint count) \
191 MAKE_ORC_UNPACK_FUNC_NAME (name) ((guint32 *) dst, src, count); \
194 /* unpack from float to float 64 (double) */
195 #define MAKE_UNPACK_FUNC_FF(name, type, FUNC) \
197 MAKE_UNPACK_FUNC_NAME (name) (type * src, gdouble * dst, gint s, \
200 for (; count; count--) \
201 *dst++ = (gdouble) FUNC (*src++); \
204 /* unpack from float to float 64 (double) with orc */
205 #define MAKE_UNPACK_FUNC_ORC_FF(name, type, FUNC) \
207 MAKE_UNPACK_FUNC_NAME (name) (type * src, gdouble * dst, gint s, \
210 MAKE_ORC_UNPACK_FUNC_NAME (name) ((gdouble *) dst, src, count); \
213 /* unpack from int to float 64 (double) */
214 #define MAKE_UNPACK_FUNC_IF(name, stride, sign, READ_FUNC) \
216 MAKE_UNPACK_FUNC_NAME (name) (guint8 * src, gdouble * dst, gint scale, \
220 for (; count; count--) { \
221 tmp = (gdouble) ((((gint32) READ_FUNC (src)) << scale) ^ (sign)); \
222 *dst++ = tmp * (1.0 / 2147483647.0); \
227 #define MAKE_UNPACK_FUNC_ORC_IF(name, stride, sign, READ_FUNC) \
229 MAKE_UNPACK_FUNC_NAME (name) (guint8 *src, gdouble *dst, \
230 gint scale, gint count) \
232 MAKE_ORC_UNPACK_FUNC_NAME (name) (dst, src, scale, count); \
235 #define READ8(p) GST_READ_UINT8(p)
236 #define READ16_FROM_LE(p) GST_READ_UINT16_LE (p)
237 #define READ16_FROM_BE(p) GST_READ_UINT16_BE (p)
238 #define READ24_FROM_LE(p) (p[0] | (p[1] << 8) | (p[2] << 16))
239 #define READ24_FROM_BE(p) (p[2] | (p[1] << 8) | (p[0] << 16))
240 #define READ32_FROM_LE(p) GST_READ_UINT32_LE (p)
241 #define READ32_FROM_BE(p) GST_READ_UINT32_BE (p)
245 MAKE_UNPACK_FUNC_ORC_II (u8, 1, SIGNED, READ8);
246 MAKE_UNPACK_FUNC_ORC_II (s8, 1, 0, READ8);
247 MAKE_UNPACK_FUNC_ORC_II (u16_le, 2, SIGNED, READ16_FROM_LE);
248 MAKE_UNPACK_FUNC_ORC_II (s16_le, 2, 0, READ16_FROM_LE);
249 MAKE_UNPACK_FUNC_ORC_II (u16_be, 2, SIGNED, READ16_FROM_BE);
250 MAKE_UNPACK_FUNC_ORC_II (s16_be, 2, 0, READ16_FROM_BE);
251 MAKE_UNPACK_FUNC_II (u24_le, 3, SIGNED, READ24_FROM_LE);
252 MAKE_UNPACK_FUNC_II (s24_le, 3, 0, READ24_FROM_LE);
253 MAKE_UNPACK_FUNC_II (u24_be, 3, SIGNED, READ24_FROM_BE);
254 MAKE_UNPACK_FUNC_II (s24_be, 3, 0, READ24_FROM_BE);
255 MAKE_UNPACK_FUNC_ORC_II (u32_le, 4, SIGNED, READ32_FROM_LE);
256 MAKE_UNPACK_FUNC_ORC_II (s32_le, 4, 0, READ32_FROM_LE);
257 MAKE_UNPACK_FUNC_ORC_II (u32_be, 4, SIGNED, READ32_FROM_BE);
258 MAKE_UNPACK_FUNC_ORC_II (s32_be, 4, 0, READ32_FROM_BE);
259 MAKE_UNPACK_FUNC_ORC_FI (float_le, gfloat, GFLOAT_FROM_LE);
260 MAKE_UNPACK_FUNC_ORC_FI (float_be, gfloat, GFLOAT_FROM_BE);
261 MAKE_UNPACK_FUNC_ORC_FI (double_le, gdouble, GDOUBLE_FROM_LE);
262 MAKE_UNPACK_FUNC_ORC_FI (double_be, gdouble, GDOUBLE_FROM_BE);
263 MAKE_UNPACK_FUNC_ORC_FF (float_hq_le, gfloat, GFLOAT_FROM_LE);
264 MAKE_UNPACK_FUNC_ORC_FF (float_hq_be, gfloat, GFLOAT_FROM_BE);
265 MAKE_UNPACK_FUNC_ORC_FF (double_hq_le, gdouble, GDOUBLE_FROM_LE);
266 MAKE_UNPACK_FUNC_ORC_FF (double_hq_be, gdouble, GDOUBLE_FROM_BE);
267 MAKE_UNPACK_FUNC_ORC_IF (u8_float, 1, SIGNED, READ8);
268 MAKE_UNPACK_FUNC_ORC_IF (s8_float, 1, 0, READ8);
269 MAKE_UNPACK_FUNC_ORC_IF (u16_le_float, 2, SIGNED, READ16_FROM_LE);
270 MAKE_UNPACK_FUNC_ORC_IF (s16_le_float, 2, 0, READ16_FROM_LE);
271 MAKE_UNPACK_FUNC_ORC_IF (u16_be_float, 2, SIGNED, READ16_FROM_BE);
272 MAKE_UNPACK_FUNC_ORC_IF (s16_be_float, 2, 0, READ16_FROM_BE);
273 MAKE_UNPACK_FUNC_IF (u24_le_float, 3, SIGNED, READ24_FROM_LE);
274 MAKE_UNPACK_FUNC_IF (s24_le_float, 3, 0, READ24_FROM_LE);
275 MAKE_UNPACK_FUNC_IF (u24_be_float, 3, SIGNED, READ24_FROM_BE);
276 MAKE_UNPACK_FUNC_IF (s24_be_float, 3, 0, READ24_FROM_BE);
277 MAKE_UNPACK_FUNC_ORC_IF (u32_le_float, 4, SIGNED, READ32_FROM_LE);
278 MAKE_UNPACK_FUNC_ORC_IF (s32_le_float, 4, 0, READ32_FROM_LE);
279 MAKE_UNPACK_FUNC_ORC_IF (u32_be_float, 4, SIGNED, READ32_FROM_BE);
280 MAKE_UNPACK_FUNC_ORC_IF (s32_be_float, 4, 0, READ32_FROM_BE);
282 /* One of the double_hq_* functions generated above is ineffecient, but it's
283 * never used anyway. The same is true for one of the s32_* functions. */
288 #define MAKE_PACK_FUNC_NAME(name) \
289 audio_convert_pack_##name
290 #define MAKE_PACK_FUNC_NAME_ORC(name) \
291 orc_audio_convert_pack_##name
294 * These functions convert the signed 32 bit integers to the
295 * target format. For this to work the following steps are done:
297 * 1) If the output format is unsigned we will XOR the sign bit. This
298 * will do the same as if we add 1<<31.
299 * 2) Afterwards we shift to the target depth. It's necessary to left-shift
300 * on signed values here to get arithmetical shifting.
301 * 3) This is then written into our target array by the corresponding write
302 * function for the target width.
305 /* pack from signed integer 32 to integer using Orc */
306 #define MAKE_PACK_FUNC_ORC_II(name, stride, sign, WRITE_FUNC) \
308 MAKE_PACK_FUNC_NAME (name) (gint32 *src, guint8 * dst, \
309 gint scale, gint count) \
311 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, scale, count); \
314 /* pack from signed integer 32 to integer */
315 #define MAKE_PACK_FUNC_II(name, stride, sign, WRITE_FUNC) \
317 MAKE_PACK_FUNC_NAME (name) (gint32 *src, guint8 * dst, \
318 gint scale, gint count) \
321 for (;count; count--) { \
322 tmp = (*src++ ^ (sign)) >> scale; \
323 WRITE_FUNC (dst, tmp); \
328 /* pack from signed integer 32 to float using orc */
329 #define MAKE_PACK_FUNC_ORC_IF(name, type, FUNC) \
331 MAKE_PACK_FUNC_NAME (name) (gint32 * src, type * dst, gint scale, \
334 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, count); \
337 /* pack from signed integer 32 to float */
338 #define MAKE_PACK_FUNC_IF(name, type, FUNC) \
340 MAKE_PACK_FUNC_NAME (name) (gint32 * src, type * dst, gint scale, \
343 for (; count; count--) \
344 *dst++ = FUNC ((type) ((*src++) * (1.0 / 2147483647.0))); \
347 /* pack from float 64 (double) to float */
348 #define MAKE_PACK_FUNC_FF(name, type, FUNC) \
350 MAKE_PACK_FUNC_NAME (name) (gdouble * src, type * dst, gint s, \
353 for (; count; count--) \
354 *dst++ = FUNC ((type) (*src++)); \
357 /* pack from float 64 (double) to float with orc */
358 #define MAKE_PACK_FUNC_ORC_FF(name, type, FUNC) \
360 MAKE_PACK_FUNC_NAME (name) (gdouble * src, type * dst, gint s, \
363 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, count); \
366 /* pack from float 64 (double) to signed int.
367 * the floats are already in the correct range. Only a cast is needed.
369 #define MAKE_PACK_FUNC_FI_S(name, stride, WRITE_FUNC) \
371 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
375 for (; count; count--) { \
376 tmp = (gint32) (*src); \
377 WRITE_FUNC (dst, tmp); \
383 /* pack from float 64 (double) to unsigned int.
384 * the floats are already in the correct range. Only a cast is needed
385 * and an addition of 2^(target_depth-1) to get in the correct unsigned
387 #define MAKE_PACK_FUNC_FI_U(name, stride, WRITE_FUNC) \
389 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
393 gdouble limit = (1U<<(32-scale-1)); \
394 for (; count; count--) { \
395 tmp = (guint32) (*src + limit); \
396 WRITE_FUNC (dst, tmp); \
402 /* pack from float 64 (double) to signed int with orc.
403 * the floats are already in the correct range. Only a cast is needed.
405 #define MAKE_PACK_FUNC_ORC_FI_S(name, stride, WRITE_FUNC) \
407 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
410 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, scale, count); \
413 /* pack from float 64 (double) to unsigned int with orc.
414 * the floats are already in the correct range. Only a cast is needed.
416 #define MAKE_PACK_FUNC_ORC_FI_U(name, stride, WRITE_FUNC) \
418 MAKE_PACK_FUNC_NAME (name) (gdouble * src, guint8 * dst, gint scale, \
421 MAKE_PACK_FUNC_NAME_ORC (name) (dst, src, scale, count); \
424 #define WRITE8(p, v) GST_WRITE_UINT8 (p, v)
425 #define WRITE16_TO_LE(p,v) GST_WRITE_UINT16_LE (p, (guint16)(v))
426 #define WRITE16_TO_BE(p,v) GST_WRITE_UINT16_BE (p, (guint16)(v))
427 #define WRITE24_TO_LE(p,v) p[0] = v & 0xff; p[1] = (v >> 8) & 0xff; p[2] = (v >> 16) & 0xff
428 #define WRITE24_TO_BE(p,v) p[2] = v & 0xff; p[1] = (v >> 8) & 0xff; p[0] = (v >> 16) & 0xff
429 #define WRITE32_TO_LE(p,v) GST_WRITE_UINT32_LE (p, (guint32)(v))
430 #define WRITE32_TO_BE(p,v) GST_WRITE_UINT32_BE (p, (guint32)(v))
432 MAKE_PACK_FUNC_ORC_II (u8, 1, SIGNED, WRITE8);
433 MAKE_PACK_FUNC_ORC_II (s8, 1, 0, WRITE8);
434 MAKE_PACK_FUNC_ORC_II (u16_le, 2, SIGNED, WRITE16_TO_LE);
435 MAKE_PACK_FUNC_ORC_II (s16_le, 2, 0, WRITE16_TO_LE);
436 MAKE_PACK_FUNC_ORC_II (u16_be, 2, SIGNED, WRITE16_TO_BE);
437 MAKE_PACK_FUNC_ORC_II (s16_be, 2, 0, WRITE16_TO_BE);
438 MAKE_PACK_FUNC_II (u24_le, 3, SIGNED, WRITE24_TO_LE);
439 MAKE_PACK_FUNC_II (s24_le, 3, 0, WRITE24_TO_LE);
440 MAKE_PACK_FUNC_II (u24_be, 3, SIGNED, WRITE24_TO_BE);
441 MAKE_PACK_FUNC_II (s24_be, 3, 0, WRITE24_TO_BE);
442 MAKE_PACK_FUNC_ORC_II (u32_le, 4, SIGNED, WRITE32_TO_LE);
443 MAKE_PACK_FUNC_ORC_II (s32_le, 4, 0, WRITE32_TO_LE);
444 MAKE_PACK_FUNC_ORC_II (u32_be, 4, SIGNED, WRITE32_TO_BE);
445 MAKE_PACK_FUNC_ORC_II (s32_be, 4, 0, WRITE32_TO_BE);
446 MAKE_PACK_FUNC_ORC_IF (float_le, gfloat, GFLOAT_TO_LE);
447 MAKE_PACK_FUNC_ORC_IF (float_be, gfloat, GFLOAT_TO_BE);
448 MAKE_PACK_FUNC_ORC_IF (double_le, gdouble, GDOUBLE_TO_LE);
449 MAKE_PACK_FUNC_ORC_IF (double_be, gdouble, GDOUBLE_TO_BE);
450 MAKE_PACK_FUNC_ORC_FF (float_hq_le, gfloat, GFLOAT_TO_LE);
451 MAKE_PACK_FUNC_ORC_FF (float_hq_be, gfloat, GFLOAT_TO_BE);
452 MAKE_PACK_FUNC_ORC_FI_U (u8_float, 1, WRITE8);
453 MAKE_PACK_FUNC_ORC_FI_S (s8_float, 1, WRITE8);
454 MAKE_PACK_FUNC_ORC_FI_U (u16_le_float, 2, WRITE16_TO_LE);
455 MAKE_PACK_FUNC_ORC_FI_S (s16_le_float, 2, WRITE16_TO_LE);
456 MAKE_PACK_FUNC_ORC_FI_U (u16_be_float, 2, WRITE16_TO_BE);
457 MAKE_PACK_FUNC_ORC_FI_S (s16_be_float, 2, WRITE16_TO_BE);
458 MAKE_PACK_FUNC_FI_U (u24_le_float, 3, WRITE24_TO_LE);
459 MAKE_PACK_FUNC_FI_S (s24_le_float, 3, WRITE24_TO_LE);
460 MAKE_PACK_FUNC_FI_U (u24_be_float, 3, WRITE24_TO_BE);
461 MAKE_PACK_FUNC_FI_S (s24_be_float, 3, WRITE24_TO_BE);
462 MAKE_PACK_FUNC_ORC_FI_U (u32_le_float, 4, WRITE32_TO_LE);
463 MAKE_PACK_FUNC_ORC_FI_S (s32_le_float, 4, WRITE32_TO_LE);
464 MAKE_PACK_FUNC_ORC_FI_U (u32_be_float, 4, WRITE32_TO_BE);
465 MAKE_PACK_FUNC_ORC_FI_S (s32_be_float, 4, WRITE32_TO_BE);
467 /* For double_hq, packing and unpacking is the same, so we reuse the unpacking
469 #define audio_convert_pack_double_hq_le MAKE_UNPACK_FUNC_NAME (double_hq_le)
470 #define audio_convert_pack_double_hq_be MAKE_UNPACK_FUNC_NAME (double_hq_be)
472 static AudioConvertUnpack unpack_funcs[] = {
473 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8),
474 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8),
475 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8),
476 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8),
477 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_le),
478 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_le),
479 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_be),
480 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_be),
481 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_le),
482 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_le),
483 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_be),
484 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_be),
485 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_le),
486 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_le),
487 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_be),
488 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_be),
489 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_le),
490 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_be),
491 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_le),
492 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_be),
493 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_hq_le),
494 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (float_hq_be),
495 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_hq_le),
496 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (double_hq_be),
497 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8_float),
498 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8_float),
499 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u8_float),
500 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s8_float),
501 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_le_float),
502 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_le_float),
503 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u16_be_float),
504 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s16_be_float),
505 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_le_float),
506 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_le_float),
507 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u24_be_float),
508 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s24_be_float),
509 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_le_float),
510 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_le_float),
511 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (u32_be_float),
512 (AudioConvertUnpack) MAKE_UNPACK_FUNC_NAME (s32_be_float),
515 static AudioConvertPack pack_funcs[] = {
516 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8),
517 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8),
518 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8),
519 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8),
520 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_le),
521 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_le),
522 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_be),
523 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_be),
524 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_le),
525 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_le),
526 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_be),
527 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_be),
528 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_le),
529 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_le),
530 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_be),
531 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_be),
532 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_le),
533 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_be),
534 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_le),
535 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_be),
536 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_hq_le),
537 (AudioConvertPack) MAKE_PACK_FUNC_NAME (float_hq_be),
538 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_hq_le),
539 (AudioConvertPack) MAKE_PACK_FUNC_NAME (double_hq_be),
540 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8_float),
541 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8_float),
542 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u8_float),
543 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s8_float),
544 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_le_float),
545 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_le_float),
546 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u16_be_float),
547 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s16_be_float),
548 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_le_float),
549 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_le_float),
550 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u24_be_float),
551 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s24_be_float),
552 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_le_float),
553 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_le_float),
554 (AudioConvertPack) MAKE_PACK_FUNC_NAME (u32_be_float),
555 (AudioConvertPack) MAKE_PACK_FUNC_NAME (s32_be_float),
558 #define DOUBLE_INTERMEDIATE_FORMAT(ctx) \
559 ((!GST_AUDIO_FORMAT_INFO_IS_INTEGER (ctx->in.finfo) && \
560 !GST_AUDIO_FORMAT_INFO_IS_INTEGER (ctx->out.finfo)) || \
561 (ctx->ns != NOISE_SHAPING_NONE))
564 audio_convert_get_func_index (AudioConvertCtx * ctx,
565 const GstAudioFormatInfo * fmt)
569 if (GST_AUDIO_FORMAT_INFO_IS_INTEGER (fmt)) {
570 index += (GST_AUDIO_FORMAT_INFO_WIDTH (fmt) / 8 - 1) * 4;
571 index += GST_AUDIO_FORMAT_INFO_IS_LITTLE_ENDIAN (fmt) ? 0 : 2;
572 index += GST_AUDIO_FORMAT_INFO_IS_SIGNED (fmt) ? 1 : 0;
573 index += (ctx->ns == NOISE_SHAPING_NONE) ? 0 : 24;
575 /* this is float/double */
577 index += (GST_AUDIO_FORMAT_INFO_WIDTH (fmt) == 32) ? 0 : 2;
578 index += GST_AUDIO_FORMAT_INFO_IS_LITTLE_ENDIAN (fmt) ? 0 : 1;
579 index += (DOUBLE_INTERMEDIATE_FORMAT (ctx)) ? 4 : 0;
585 static inline gboolean
586 check_default (AudioConvertCtx * ctx, const GstAudioFormatInfo * fmt)
588 if (!DOUBLE_INTERMEDIATE_FORMAT (ctx)) {
589 return GST_AUDIO_FORMAT_INFO_FORMAT (fmt) == GST_AUDIO_FORMAT_S32;
591 return GST_AUDIO_FORMAT_INFO_FORMAT (fmt) == GST_AUDIO_FORMAT_F64;
596 audio_convert_prepare_context (AudioConvertCtx * ctx, GstAudioInfo * in,
597 GstAudioInfo * out, GstAudioConvertDithering dither,
598 GstAudioConvertNoiseShaping ns)
600 gint idx_in, idx_out;
601 gint in_depth, out_depth;
603 g_return_val_if_fail (ctx != NULL, FALSE);
604 g_return_val_if_fail (in != NULL, FALSE);
605 g_return_val_if_fail (out != NULL, FALSE);
607 /* first clean the existing context */
608 audio_convert_clean_context (ctx);
613 in_depth = GST_AUDIO_FORMAT_INFO_DEPTH (in->finfo);
614 out_depth = GST_AUDIO_FORMAT_INFO_DEPTH (out->finfo);
616 /* Don't dither or apply noise shaping if target depth is bigger than 20 bits
617 * as DA converters only can do a SNR up to 20 bits in reality.
618 * Also don't dither or apply noise shaping if target depth is larger than
620 if (out_depth <= 20 && (!GST_AUDIO_FORMAT_INFO_IS_INTEGER (in->finfo)
621 || in_depth >= out_depth)) {
622 ctx->dither = dither;
625 ctx->dither = DITHER_NONE;
626 ctx->ns = NOISE_SHAPING_NONE;
629 /* Use simple error feedback when output sample rate is smaller than
630 * 32000 as the other methods might move the noise to audible ranges */
631 if (ctx->ns > NOISE_SHAPING_ERROR_FEEDBACK && out->rate < 32000)
632 ctx->ns = NOISE_SHAPING_ERROR_FEEDBACK;
634 gst_channel_mix_setup_matrix (ctx);
636 idx_in = audio_convert_get_func_index (ctx, in->finfo);
637 ctx->unpack = unpack_funcs[idx_in];
639 idx_out = audio_convert_get_func_index (ctx, out->finfo);
640 ctx->pack = pack_funcs[idx_out];
642 /* if both formats are float/double or we use noise shaping use double as
643 * intermediate format and and switch mixing */
644 if (!DOUBLE_INTERMEDIATE_FORMAT (ctx)) {
645 GST_INFO ("use int mixing");
646 ctx->channel_mix = (AudioConvertMix) gst_channel_mix_mix_int;
648 GST_INFO ("use float mixing");
649 ctx->channel_mix = (AudioConvertMix) gst_channel_mix_mix_float;
651 GST_INFO ("unitsizes: %d -> %d", in->bpf, out->bpf);
653 /* check if input is in default format */
654 ctx->in_default = check_default (ctx, in->finfo);
655 /* check if channel mixer is passthrough */
656 ctx->mix_passthrough = gst_channel_mix_passthrough (ctx);
657 /* check if output is in default format */
658 ctx->out_default = check_default (ctx, out->finfo);
660 GST_INFO ("in default %d, mix passthrough %d, out default %d",
661 ctx->in_default, ctx->mix_passthrough, ctx->out_default);
664 GST_AUDIO_FORMAT_INFO_IS_INTEGER (in->finfo) ? (32 - in_depth) : 0;
666 GST_AUDIO_FORMAT_INFO_IS_INTEGER (out->finfo) ? (32 - out_depth) : 0;
668 gst_audio_quantize_setup (ctx);
674 audio_convert_clean_context (AudioConvertCtx * ctx)
676 g_return_val_if_fail (ctx != NULL, FALSE);
678 gst_audio_quantize_free (ctx);
679 gst_audio_info_init (&ctx->in);
680 gst_audio_info_init (&ctx->out);
681 gst_channel_mix_unset_matrix (ctx);
683 g_free (ctx->tmpbuf);
691 audio_convert_get_sizes (AudioConvertCtx * ctx, gint samples, gint * srcsize,
694 g_return_val_if_fail (ctx != NULL, FALSE);
697 *srcsize = samples * ctx->in.bpf;
699 *dstsize = samples * ctx->out.bpf;
705 audio_convert_convert (AudioConvertCtx * ctx, gpointer src,
706 gpointer dst, gint samples, gboolean src_writable)
708 guint insize, outsize, size;
709 gpointer outbuf, tmpbuf;
710 guint intemp = 0, outtemp = 0, biggest;
711 gint in_width, out_width;
713 g_return_val_if_fail (ctx != NULL, FALSE);
714 g_return_val_if_fail (src != NULL, FALSE);
715 g_return_val_if_fail (dst != NULL, FALSE);
716 g_return_val_if_fail (samples >= 0, FALSE);
721 insize = ctx->in.bpf * samples;
722 outsize = ctx->out.bpf * samples;
724 in_width = GST_AUDIO_FORMAT_INFO_WIDTH (ctx->in.finfo);
725 out_width = GST_AUDIO_FORMAT_INFO_WIDTH (ctx->out.finfo);
727 /* find biggest temp buffer size */
728 size = (DOUBLE_INTERMEDIATE_FORMAT (ctx)) ? sizeof (gdouble)
731 if (!ctx->in_default)
732 intemp = gst_util_uint64_scale (insize, size * 8, in_width);
733 if (!ctx->mix_passthrough || !ctx->out_default)
734 outtemp = gst_util_uint64_scale (outsize, size * 8, out_width);
735 biggest = MAX (intemp, outtemp);
737 /* see if one of the buffers can be used as temp */
738 if ((outsize >= biggest) && (ctx->out.bpf <= size))
740 else if ((insize >= biggest) && src_writable && (ctx->in.bpf >= size))
743 if (biggest > ctx->tmpbufsize) {
744 ctx->tmpbuf = g_realloc (ctx->tmpbuf, biggest);
745 ctx->tmpbufsize = biggest;
747 tmpbuf = ctx->tmpbuf;
750 /* start conversion */
751 if (!ctx->in_default) {
752 /* check if final conversion */
753 if (!(ctx->out_default && ctx->mix_passthrough))
758 /* unpack to default format */
759 ctx->unpack (src, outbuf, ctx->in_scale, samples * ctx->in.channels);
764 if (!ctx->mix_passthrough) {
765 /* check if final conversion */
766 if (!ctx->out_default)
771 /* convert channels */
772 ctx->channel_mix (ctx, src, outbuf, samples);
777 /* we only need to quantize if output format is int */
778 if (GST_AUDIO_FORMAT_INFO_IS_INTEGER (ctx->out.finfo)) {
779 if (ctx->out_default)
783 ctx->quantize (ctx, src, outbuf, samples);
786 if (!ctx->out_default) {
787 /* pack default format into dst */
788 ctx->pack (src, dst, ctx->out_scale, samples * ctx->out.channels);