MythTV  master
audioconvert.cpp
Go to the documentation of this file.
1 
2 /*
3  * Class AudioConvert
4  * Created by Jean-Yves Avenard on 10/06/13.
5  *
6  * Copyright (C) Bubblestuff Pty Ltd 2013
7  * Copyright (C) foobum@gmail.com 2010
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <cinttypes>
25 #include <cmath>
26 #include <sys/types.h>
27 
28 #include "mythconfig.h"
29 #include "mythlogging.h"
30 #include "audioconvert.h"
31 
32 extern "C" {
33 #include "libavcodec/avcodec.h"
34 #include "libswresample/swresample.h"
35 }
36 
37 #define LOC QString("AudioConvert: ")
38 
39 #define ISALIGN(x) (((unsigned long)(x) & 0xf) == 0)
40 
41 #if ARCH_X86
42 static int has_sse2 = -1;
43 
44 // Check cpuid for SSE2 support on x86 / x86_64
45 static inline bool sse_check()
46 {
47  if (has_sse2 != -1)
48  return (bool)has_sse2;
49  __asm__(
50  // -fPIC - we may not clobber ebx/rbx
51 #if ARCH_X86_64
52  "push %%rbx \n\t"
53 #else
54  "push %%ebx \n\t"
55 #endif
56  "mov $1, %%eax \n\t"
57  "cpuid \n\t"
58  "and $0x4000000, %%edx \n\t"
59  "shr $26, %%edx \n\t"
60 #if ARCH_X86_64
61  "pop %%rbx \n\t"
62 #else
63  "pop %%ebx \n\t"
64 #endif
65  :"=d"(has_sse2)
66  ::"%eax","%ecx"
67  );
68  return (bool)has_sse2;
69 }
70 #endif //ARCH_x86
71 
72 #if !HAVE_LRINTF
73 static av_always_inline av_const long int lrintf(float x)
74 {
75  return (int)(rint(x));
76 }
77 #endif /* HAVE_LRINTF */
78 
79 static inline float clipcheck(float f)
80 {
81  if (f > 1.0F) f = 1.0F;
82  else if (f < -1.0F) f = -1.0F;
83  return f;
84 }
85 
86 /*
87  The SSE code processes 16 bytes at a time and leaves any remainder for the C
88  */
89 
90 static int toFloat8(float* out, const uchar* in, int len)
91 {
92  int i = 0;
93  float f = 1.0F / ((1<<7));
94 
95 #if ARCH_X86
96  if (sse_check() && len >= 16)
97  {
98  int loops = len >> 4;
99  i = loops << 4;
100  int a = 0x80808080;
101 
102  __asm__ volatile (
103  "movd %3, %%xmm0 \n\t"
104  "movd %4, %%xmm7 \n\t"
105  "punpckldq %%xmm0, %%xmm0 \n\t"
106  "punpckldq %%xmm7, %%xmm7 \n\t"
107  "punpckldq %%xmm0, %%xmm0 \n\t"
108  "punpckldq %%xmm7, %%xmm7 \n\t"
109  "1: \n\t"
110  "movdqu (%1), %%xmm1 \n\t"
111  "xorpd %%xmm2, %%xmm2 \n\t"
112  "xorpd %%xmm3, %%xmm3 \n\t"
113  "psubb %%xmm0, %%xmm1 \n\t"
114  "xorpd %%xmm4, %%xmm4 \n\t"
115  "punpcklbw %%xmm1, %%xmm2 \n\t"
116  "xorpd %%xmm5, %%xmm5 \n\t"
117  "punpckhbw %%xmm1, %%xmm3 \n\t"
118  "punpcklwd %%xmm2, %%xmm4 \n\t"
119  "xorpd %%xmm6, %%xmm6 \n\t"
120  "punpckhwd %%xmm2, %%xmm5 \n\t"
121  "psrad $24, %%xmm4 \n\t"
122  "punpcklwd %%xmm3, %%xmm6 \n\t"
123  "psrad $24, %%xmm5 \n\t"
124  "punpckhwd %%xmm3, %%xmm1 \n\t"
125  "psrad $24, %%xmm6 \n\t"
126  "cvtdq2ps %%xmm4, %%xmm4 \n\t"
127  "psrad $24, %%xmm1 \n\t"
128  "cvtdq2ps %%xmm5, %%xmm5 \n\t"
129  "mulps %%xmm7, %%xmm4 \n\t"
130  "cvtdq2ps %%xmm6, %%xmm6 \n\t"
131  "mulps %%xmm7, %%xmm5 \n\t"
132  "movups %%xmm4, (%0) \n\t"
133  "cvtdq2ps %%xmm1, %%xmm1 \n\t"
134  "mulps %%xmm7, %%xmm6 \n\t"
135  "movups %%xmm5, 16(%0) \n\t"
136  "mulps %%xmm7, %%xmm1 \n\t"
137  "movups %%xmm6, 32(%0) \n\t"
138  "add $16, %1 \n\t"
139  "movups %%xmm1, 48(%0) \n\t"
140  "add $64, %0 \n\t"
141  "sub $1, %%ecx \n\t"
142  "jnz 1b \n\t"
143  :"+r"(out),"+r"(in)
144  :"c"(loops), "r"(a), "r"(f)
145  );
146  }
147 #endif //ARCH_x86
148  for (; i < len; i++)
149  *out++ = (*in++ - 0x80) * f;
150  return len << 2;
151 }
152 
153 /*
154  The SSE code processes 16 bytes at a time and leaves any remainder for the C
155  - there is no remainder in practice */
156 
157 static inline uchar clip_uchar(int a)
158 {
159  if (a&(~0xFF))
160  return (-a)>>31;
161  return a;
162 }
163 
164 static int fromFloat8(uchar* out, const float* in, int len)
165 {
166  int i = 0;
167  float f = (1<<7);
168 
169 #if ARCH_X86
170  if (sse_check() && len >= 16)
171  {
172  int loops = len >> 4;
173  i = loops << 4;
174  int a = 0x80808080;
175 
176  __asm__ volatile (
177  "movd %3, %%xmm0 \n\t"
178  "movd %4, %%xmm7 \n\t"
179  "punpckldq %%xmm0, %%xmm0 \n\t"
180  "punpckldq %%xmm7, %%xmm7 \n\t"
181  "punpckldq %%xmm0, %%xmm0 \n\t"
182  "punpckldq %%xmm7, %%xmm7 \n\t"
183  "1: \n\t"
184  "movups (%1), %%xmm1 \n\t"
185  "movups 16(%1), %%xmm2 \n\t"
186  "mulps %%xmm7, %%xmm1 \n\t"
187  "movups 32(%1), %%xmm3 \n\t"
188  "mulps %%xmm7, %%xmm2 \n\t"
189  "cvtps2dq %%xmm1, %%xmm1 \n\t"
190  "movups 48(%1), %%xmm4 \n\t"
191  "mulps %%xmm7, %%xmm3 \n\t"
192  "cvtps2dq %%xmm2, %%xmm2 \n\t"
193  "mulps %%xmm7, %%xmm4 \n\t"
194  "cvtps2dq %%xmm3, %%xmm3 \n\t"
195  "packssdw %%xmm2, %%xmm1 \n\t"
196  "cvtps2dq %%xmm4, %%xmm4 \n\t"
197  "packssdw %%xmm4, %%xmm3 \n\t"
198  "add $64, %1 \n\t"
199  "packsswb %%xmm3, %%xmm1 \n\t"
200  "paddb %%xmm0, %%xmm1 \n\t"
201  "movdqu %%xmm1, (%0) \n\t"
202  "add $16, %0 \n\t"
203  "sub $1, %%ecx \n\t"
204  "jnz 1b \n\t"
205  :"+r"(out),"+r"(in)
206  :"c"(loops), "r"(a), "r"(f)
207  );
208  }
209 #endif //ARCH_x86
210  for (;i < len; i++)
211  *out++ = clip_uchar(lrintf(*in++ * f) + 0x80);
212  return len;
213 }
214 
215 static int toFloat16(float* out, const short* in, int len)
216 {
217  int i = 0;
218  float f = 1.0F / ((1<<15));
219 
220 #if ARCH_X86
221  if (sse_check() && len >= 16)
222  {
223  int loops = len >> 4;
224  i = loops << 4;
225 
226  __asm__ volatile (
227  "movd %3, %%xmm7 \n\t"
228  "punpckldq %%xmm7, %%xmm7 \n\t"
229  "punpckldq %%xmm7, %%xmm7 \n\t"
230  "1: \n\t"
231  "xorpd %%xmm2, %%xmm2 \n\t"
232  "movdqu (%1), %%xmm1 \n\t"
233  "xorpd %%xmm3, %%xmm3 \n\t"
234  "punpcklwd %%xmm1, %%xmm2 \n\t"
235  "movdqu 16(%1), %%xmm4 \n\t"
236  "punpckhwd %%xmm1, %%xmm3 \n\t"
237  "psrad $16, %%xmm2 \n\t"
238  "punpcklwd %%xmm4, %%xmm5 \n\t"
239  "psrad $16, %%xmm3 \n\t"
240  "cvtdq2ps %%xmm2, %%xmm2 \n\t"
241  "punpckhwd %%xmm4, %%xmm6 \n\t"
242  "psrad $16, %%xmm5 \n\t"
243  "mulps %%xmm7, %%xmm2 \n\t"
244  "cvtdq2ps %%xmm3, %%xmm3 \n\t"
245  "psrad $16, %%xmm6 \n\t"
246  "mulps %%xmm7, %%xmm3 \n\t"
247  "cvtdq2ps %%xmm5, %%xmm5 \n\t"
248  "movups %%xmm2, (%0) \n\t"
249  "cvtdq2ps %%xmm6, %%xmm6 \n\t"
250  "mulps %%xmm7, %%xmm5 \n\t"
251  "movups %%xmm3, 16(%0) \n\t"
252  "mulps %%xmm7, %%xmm6 \n\t"
253  "movups %%xmm5, 32(%0) \n\t"
254  "add $32, %1 \n\t"
255  "movups %%xmm6, 48(%0) \n\t"
256  "add $64, %0 \n\t"
257  "sub $1, %%ecx \n\t"
258  "jnz 1b \n\t"
259  :"+r"(out),"+r"(in)
260  :"c"(loops), "r"(f)
261  );
262  }
263 #endif //ARCH_x86
264  for (; i < len; i++)
265  *out++ = *in++ * f;
266  return len << 2;
267 }
268 
269 static inline short clip_short(int a)
270 {
271  if ((a+0x8000) & ~0xFFFF)
272  return (a>>31) ^ 0x7FFF;
273  return a;
274 }
275 
276 static int fromFloat16(short* out, const float* in, int len)
277 {
278  int i = 0;
279  float f = (1<<15);
280 
281 #if ARCH_X86
282  if (sse_check() && len >= 16)
283  {
284  int loops = len >> 4;
285  i = loops << 4;
286 
287  __asm__ volatile (
288  "movd %3, %%xmm7 \n\t"
289  "punpckldq %%xmm7, %%xmm7 \n\t"
290  "punpckldq %%xmm7, %%xmm7 \n\t"
291  "1: \n\t"
292  "movups (%1), %%xmm1 \n\t"
293  "movups 16(%1), %%xmm2 \n\t"
294  "mulps %%xmm7, %%xmm1 \n\t"
295  "movups 32(%1), %%xmm3 \n\t"
296  "mulps %%xmm7, %%xmm2 \n\t"
297  "cvtps2dq %%xmm1, %%xmm1 \n\t"
298  "movups 48(%1), %%xmm4 \n\t"
299  "mulps %%xmm7, %%xmm3 \n\t"
300  "cvtps2dq %%xmm2, %%xmm2 \n\t"
301  "mulps %%xmm7, %%xmm4 \n\t"
302  "cvtps2dq %%xmm3, %%xmm3 \n\t"
303  "cvtps2dq %%xmm4, %%xmm4 \n\t"
304  "packssdw %%xmm2, %%xmm1 \n\t"
305  "packssdw %%xmm4, %%xmm3 \n\t"
306  "add $64, %1 \n\t"
307  "movdqu %%xmm1, (%0) \n\t"
308  "movdqu %%xmm3, 16(%0) \n\t"
309  "add $32, %0 \n\t"
310  "sub $1, %%ecx \n\t"
311  "jnz 1b \n\t"
312  :"+r"(out),"+r"(in)
313  :"c"(loops), "r"(f)
314  );
315  }
316 #endif //ARCH_x86
317  for (;i < len;i++)
318  *out++ = clip_short(lrintf(*in++ * f));
319  return len << 1;
320 }
321 
322 static int toFloat32(AudioFormat format, float* out, const int* in, int len)
323 {
324  int i = 0;
325  int bits = AudioOutputSettings::FormatToBits(format);
326  float f = 1.0F / ((uint)(1<<(bits-1)));
327  int shift = 32 - bits;
328 
329  if (format == FORMAT_S24LSB)
330  shift = 0;
331 
332 #if ARCH_X86
333  if (sse_check() && len >= 16)
334  {
335  int loops = len >> 4;
336  i = loops << 4;
337 
338  __asm__ volatile (
339  "movd %3, %%xmm7 \n\t"
340  "punpckldq %%xmm7, %%xmm7 \n\t"
341  "movd %4, %%xmm6 \n\t"
342  "punpckldq %%xmm7, %%xmm7 \n\t"
343  "1: \n\t"
344  "movdqu (%1), %%xmm1 \n\t"
345  "movdqu 16(%1), %%xmm2 \n\t"
346  "psrad %%xmm6, %%xmm1 \n\t"
347  "movdqu 32(%1), %%xmm3 \n\t"
348  "cvtdq2ps %%xmm1, %%xmm1 \n\t"
349  "psrad %%xmm6, %%xmm2 \n\t"
350  "movdqu 48(%1), %%xmm4 \n\t"
351  "cvtdq2ps %%xmm2, %%xmm2 \n\t"
352  "psrad %%xmm6, %%xmm3 \n\t"
353  "mulps %%xmm7, %%xmm1 \n\t"
354  "psrad %%xmm6, %%xmm4 \n\t"
355  "cvtdq2ps %%xmm3, %%xmm3 \n\t"
356  "movups %%xmm1, (%0) \n\t"
357  "mulps %%xmm7, %%xmm2 \n\t"
358  "cvtdq2ps %%xmm4, %%xmm4 \n\t"
359  "movups %%xmm2, 16(%0) \n\t"
360  "mulps %%xmm7, %%xmm3 \n\t"
361  "mulps %%xmm7, %%xmm4 \n\t"
362  "movups %%xmm3, 32(%0) \n\t"
363  "add $64, %1 \n\t"
364  "movups %%xmm4, 48(%0) \n\t"
365  "add $64, %0 \n\t"
366  "sub $1, %%ecx \n\t"
367  "jnz 1b \n\t"
368  :"+r"(out),"+r"(in)
369  :"c"(loops), "r"(f), "r"(shift)
370  );
371  }
372 #endif //ARCH_x86
373  for (; i < len; i++)
374  *out++ = (*in++ >> shift) * f;
375  return len << 2;
376 }
377 
378 static int fromFloat32(AudioFormat format, int* out, const float* in, int len)
379 {
380  int i = 0;
381  int bits = AudioOutputSettings::FormatToBits(format);
382  float f = (uint)(1<<(bits-1));
383  int shift = 32 - bits;
384 
385  if (format == FORMAT_S24LSB)
386  shift = 0;
387 
388 #if ARCH_X86
389  if (sse_check() && len >= 16)
390  {
391  float o = 0.99999995;
392  float mo = -1;
393  int loops = len >> 4;
394  i = loops << 4;
395 
396  __asm__ volatile (
397  "movd %3, %%xmm7 \n\t"
398  "movss %4, %%xmm5 \n\t"
399  "punpckldq %%xmm7, %%xmm7 \n\t"
400  "movss %5, %%xmm6 \n\t"
401  "punpckldq %%xmm5, %%xmm5 \n\t"
402  "punpckldq %%xmm6, %%xmm6 \n\t"
403  "movd %6, %%xmm0 \n\t"
404  "punpckldq %%xmm7, %%xmm7 \n\t"
405  "punpckldq %%xmm5, %%xmm5 \n\t"
406  "punpckldq %%xmm6, %%xmm6 \n\t"
407  "1: \n\t"
408  "movups (%1), %%xmm1 \n\t"
409  "movups 16(%1), %%xmm2 \n\t"
410  "minps %%xmm5, %%xmm1 \n\t"
411  "movups 32(%1), %%xmm3 \n\t"
412  "maxps %%xmm6, %%xmm1 \n\t"
413  "movups 48(%1), %%xmm4 \n\t"
414  "mulps %%xmm7, %%xmm1 \n\t"
415  "minps %%xmm5, %%xmm2 \n\t"
416  "cvtps2dq %%xmm1, %%xmm1 \n\t"
417  "maxps %%xmm6, %%xmm2 \n\t"
418  "pslld %%xmm0, %%xmm1 \n\t"
419  "minps %%xmm5, %%xmm3 \n\t"
420  "mulps %%xmm7, %%xmm2 \n\t"
421  "movdqu %%xmm1, (%0) \n\t"
422  "cvtps2dq %%xmm2, %%xmm2 \n\t"
423  "maxps %%xmm6, %%xmm3 \n\t"
424  "minps %%xmm5, %%xmm4 \n\t"
425  "pslld %%xmm0, %%xmm2 \n\t"
426  "mulps %%xmm7, %%xmm3 \n\t"
427  "maxps %%xmm6, %%xmm4 \n\t"
428  "movdqu %%xmm2, 16(%0) \n\t"
429  "cvtps2dq %%xmm3, %%xmm3 \n\t"
430  "mulps %%xmm7, %%xmm4 \n\t"
431  "pslld %%xmm0, %%xmm3 \n\t"
432  "cvtps2dq %%xmm4, %%xmm4 \n\t"
433  "movdqu %%xmm3, 32(%0) \n\t"
434  "pslld %%xmm0, %%xmm4 \n\t"
435  "add $64, %1 \n\t"
436  "movdqu %%xmm4, 48(%0) \n\t"
437  "add $64, %0 \n\t"
438  "sub $1, %%ecx \n\t"
439  "jnz 1b \n\t"
440  :"+r"(out), "+r"(in)
441  :"c"(loops), "r"(f), "m"(o), "m"(mo), "r"(shift)
442  );
443  }
444 #endif //ARCH_x86
445  uint range = 1<<(bits-1);
446  for (; i < len; i++)
447  {
448  float valf = *in++;
449 
450  if (valf >= 1.0F)
451  {
452  *out++ = (range - 128) << shift;
453  continue;
454  }
455  if (valf <= -1.0F)
456  {
457  *out++ = (-range) << shift;
458  continue;
459  }
460  *out++ = lrintf(valf * f) << shift;
461  }
462  return len << 2;
463 }
464 
465 static int fromFloatFLT(float* out, const float* in, int len)
466 {
467  int i = 0;
468 
469 #if ARCH_X86
470  if (sse_check() && len >= 16)
471  {
472  int loops = len >> 4;
473  float o = 1;
474  float mo = -1;
475  i = loops << 4;
476 
477  __asm__ volatile (
478  "movss %3, %%xmm6 \n\t"
479  "movss %4, %%xmm7 \n\t"
480  "punpckldq %%xmm6, %%xmm6 \n\t"
481  "punpckldq %%xmm7, %%xmm7 \n\t"
482  "punpckldq %%xmm6, %%xmm6 \n\t"
483  "punpckldq %%xmm7, %%xmm7 \n\t"
484  "1: \n\t"
485  "movups (%1), %%xmm1 \n\t"
486  "movups 16(%1), %%xmm2 \n\t"
487  "minps %%xmm6, %%xmm1 \n\t"
488  "movups 32(%1), %%xmm3 \n\t"
489  "maxps %%xmm7, %%xmm1 \n\t"
490  "minps %%xmm6, %%xmm2 \n\t"
491  "movups 48(%1), %%xmm4 \n\t"
492  "maxps %%xmm7, %%xmm2 \n\t"
493  "movups %%xmm1, (%0) \n\t"
494  "minps %%xmm6, %%xmm3 \n\t"
495  "movups %%xmm2, 16(%0) \n\t"
496  "maxps %%xmm7, %%xmm3 \n\t"
497  "minps %%xmm6, %%xmm4 \n\t"
498  "movups %%xmm3, 32(%0) \n\t"
499  "maxps %%xmm7, %%xmm4 \n\t"
500  "add $64, %1 \n\t"
501  "movups %%xmm4, 48(%0) \n\t"
502  "add $64, %0 \n\t"
503  "sub $1, %%ecx \n\t"
504  "jnz 1b \n\t"
505  :"+r"(out), "+r"(in)
506  :"c"(loops), "m"(o), "m"(mo)
507  );
508  }
509 #endif //ARCH_x86
510  for (;i < len;i++)
511  *out++ = clipcheck(*in++);
512  return len << 2;
513 }
514 
520 int AudioConvert::toFloat(AudioFormat format, void* out, const void* in,
521  int bytes)
522 {
523  if (bytes <= 0)
524  return 0;
525 
526  switch (format)
527  {
528  case FORMAT_U8:
529  return toFloat8((float*)out, (uchar*)in, bytes);
530  case FORMAT_S16:
531  return toFloat16((float*)out, (short*)in, bytes >> 1);
532  case FORMAT_S24:
533  case FORMAT_S24LSB:
534  case FORMAT_S32:
535  return toFloat32(format, (float*)out, (int*)in, bytes >> 2);
536  case FORMAT_FLT:
537  memcpy(out, in, bytes);
538  return bytes;
539  case FORMAT_NONE:
540  default:
541  return 0;
542  }
543 }
544 
550 int AudioConvert::fromFloat(AudioFormat format, void* out, const void* in,
551  int bytes)
552 {
553  if (bytes <= 0)
554  return 0;
555 
556  switch (format)
557  {
558  case FORMAT_U8:
559  return fromFloat8((uchar*)out, (float*)in, bytes >> 2);
560  case FORMAT_S16:
561  return fromFloat16((short*)out, (float*)in, bytes >> 2);
562  case FORMAT_S24:
563  case FORMAT_S24LSB:
564  case FORMAT_S32:
565  return fromFloat32(format, (int*)out, (float*)in, bytes >> 2);
566  case FORMAT_FLT:
567  return fromFloatFLT((float*)out, (float*)in, bytes >> 2);
568  case FORMAT_NONE:
569  default:
570  return 0;
571  }
572 }
573 
575 
577 {
578 public:
579  AudioConvertInternal(AVSampleFormat in, AVSampleFormat out) :
580  m_in(in), m_out(out)
581  {
582  m_swr = swr_alloc_set_opts(nullptr,
583  av_get_default_channel_layout(1),
584  m_out,
585  48000,
586  av_get_default_channel_layout(1),
587  m_in,
588  48000,
589  0, nullptr);
590  if (!m_swr)
591  {
592  LOG(VB_AUDIO, LOG_ERR, LOC + "error allocating resampler context");
593  return;
594  }
595  /* initialize the resampling context */
596  int ret = swr_init(m_swr);
597  if (ret < 0)
598  {
599  char error[AV_ERROR_MAX_STRING_SIZE];
600  LOG(VB_AUDIO, LOG_ERR, LOC +
601  QString("error initializing resampler context (%1)")
602  .arg(av_make_error_string(error, sizeof(error), ret)));
603  swr_free(&m_swr);
604  return;
605  }
606  }
607  int Process(void* out, const void* in, int bytes) const
608  {
609  if (!m_swr)
610  return -1;
611 
612  uint8_t* outp[] = {(uint8_t*)out};
613  const uint8_t* inp[] = {(const uint8_t*)in};
614  int samples = bytes / av_get_bytes_per_sample(m_in);
615  int ret = swr_convert(m_swr,
616  outp, samples,
617  inp, samples);
618  if (ret < 0)
619  return ret;
620  return ret * av_get_bytes_per_sample(m_out);
621  }
623  {
624  if (m_swr)
625  {
626  swr_free(&m_swr);
627  }
628  }
629 
630  SwrContext* m_swr {nullptr};
631  AVSampleFormat m_in, m_out;
632 };
633 
634 
636 {
637  delete m_ctx;
638  m_ctx = nullptr;
639 }
640 
647 int AudioConvert::Process(void* out, const void* in, int bytes, bool noclip)
648 {
649  if (bytes <= 0)
650  return 0;
651  if (m_out == FORMAT_NONE || m_in == FORMAT_NONE)
652  return 0;
653 
654  if (noclip && m_in == m_out)
655  {
656  memcpy(out, in, bytes);
657  return bytes;
658  }
659 
660  /* use conversion routines to perform clipping on samples */
661  if (m_in == FORMAT_FLT)
662  return fromFloat(m_out, out, in, bytes);
663  if (m_out == FORMAT_FLT)
664  return toFloat(m_in, out, in, bytes);
665 
666  if (m_in == m_out)
667  {
668  memcpy(out, in, bytes);
669  return bytes;
670  }
671 
672  if (m_in == FORMAT_S24 || m_in == FORMAT_S24LSB ||
674  {
675  // FFmpeg can't handle those, so use float conversion intermediary
677  {
678  // this can be done in place
679  int s = toFloat(m_in, out, in, bytes);
680  return fromFloat(m_out, out, out, s);
681  }
682  // this leave S24 -> U8/S16.
683  // TODO: native handling of those ; use internal temp buffer in the mean time
684  alignas(16) uint8_t buffer[65536] {0};
685  int left = bytes;
686 
687  while (left > 0)
688  {
689  int s = 0;
690 
691  if (left >= 65536)
692  {
693  s = toFloat(m_in, buffer, in, 65536);
694  in = (void*)((long)in + s);
695  out = (void*)((long)out + fromFloat(m_out, out, buffer, s));
696  left -= 65536;
697  continue;
698  }
699  s = toFloat(m_in, buffer, in, left);
700  in = (void*)((long)in + s);
701  out = (void*)((long)out + fromFloat(m_out, out, buffer, s));
702  left = 0;
703  }
705  }
706 
707  // use FFmpeg conversion routine for S32<->S16, S32<->U8 and S16<->U8
708  if (!m_ctx)
709  {
712  }
713 
714  return m_ctx->Process(out, in, bytes);
715 }
716 
720 void AudioConvert::MonoToStereo(void* dst, const void* src, int samples)
721 {
722  auto* d = (float*)dst;
723  auto* s = (float*)src;
724  for (int i = 0; i < samples; i++)
725  {
726  *d++ = *s;
727  *d++ = *s++;
728  }
729 }
730 
731 template <class AudioDataType>
732 void tDeinterleaveSample(AudioDataType* out, const AudioDataType* in, int channels, int frames)
733 {
734  AudioDataType* outp[8];
735 
736  for (int i = 0; i < channels; i++)
737  {
738  outp[i] = out + (i * frames);
739  }
740 
741  for (int i = 0; i < frames; i++)
742  {
743  for (int j = 0; j < channels; j++)
744  {
745  *(outp[j]++) = *(in++);
746  }
747  }
748 }
749 
755  uint8_t* output, const uint8_t* input,
756  int data_size)
757 {
758  if (channels == 1)
759  {
760  // If channel count is 1, planar and non-planar formats are the same
761  memcpy(output, input, data_size);
762  return;
763  }
764 
765  int bits = AudioOutputSettings::FormatToBits(format);
766  if (bits == 8)
767  {
768  tDeinterleaveSample((char*)output, (const char*)input, channels, data_size/sizeof(char)/channels);
769  }
770  else if (bits == 16)
771  {
772  tDeinterleaveSample((short*)output, (const short*)input, channels, data_size/sizeof(short)/channels);
773  }
774  else
775  {
776  tDeinterleaveSample((int*)output, (const int*)input, channels, data_size/sizeof(int)/channels);
777  }
778 }
779 
780 template <class AudioDataType>
781 void tInterleaveSample(AudioDataType* out, const AudioDataType* in, int channels, int frames,
782  const AudioDataType* const* inp = nullptr)
783 {
784  const AudioDataType* my_inp[8];
785 
786  if (channels == 1)
787  {
788  //special case for mono
789  memcpy(out, inp ? inp[0] : in, sizeof(AudioDataType) * frames);
790  return;
791  }
792 
793  if (!inp)
794  {
795  // We're given an array of int, calculate pointers to each row
796  for (int i = 0; i < channels; i++)
797  {
798  my_inp[i] = in + (i * frames);
799  }
800  }
801  else
802  {
803  for (int i = 0; i < channels; i++)
804  {
805  my_inp[i] = inp[i];
806  }
807  }
808 
809  for (int i = 0; i < frames; i++)
810  {
811  for (int j = 0; j < channels; j++)
812  {
813  *(out++) = *(my_inp[j]++);
814  }
815  }
816 }
817 
824  uint8_t* output, const uint8_t* const* input,
825  int data_size)
826 {
827  int bits = AudioOutputSettings::FormatToBits(format);
828  if (bits == 8)
829  {
830  tInterleaveSample((char*)output, (const char*)nullptr, channels, data_size/sizeof(char)/channels,
831  (const char* const*)input);
832  }
833  else if (bits == 16)
834  {
835  tInterleaveSample((short*)output, (const short*)nullptr, channels, data_size/sizeof(short)/channels,
836  (const short* const*)input);
837  }
838  else
839  {
840  tInterleaveSample((int*)output, (const int*)nullptr, channels, data_size/sizeof(int)/channels,
841  (const int* const*)input);
842  }
843 }
844 
850  uint8_t* output, const uint8_t* input,
851  int data_size)
852 {
853  int bits = AudioOutputSettings::FormatToBits(format);
854  if (bits == 8)
855  {
856  tInterleaveSample((char*)output, (const char*)input, channels, data_size/sizeof(char)/channels);
857  }
858  else if (bits == 16)
859  {
860  tInterleaveSample((short*)output, (const short*)input, channels, data_size/sizeof(short)/channels);
861  }
862  else
863  {
864  tInterleaveSample((int*)output, (const int*)input, channels, data_size/sizeof(int)/channels);
865  }
866 }
867 
869  uint8_t* output, const uint8_t* input,
870  int data_size)
871 {
872  DeinterleaveSamples(m_in, channels, output, input, data_size);
873 }
874 
876  uint8_t* output, const uint8_t* const* input,
877  int data_size)
878 {
879  InterleaveSamples(m_in, channels, output, input, data_size);
880 }
881 
883  uint8_t* output, const uint8_t* input,
884  int data_size)
885 {
886  InterleaveSamples(m_in, channels, output, input, data_size);
887 }
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
int Process(void *out, const void *in, int bytes) const
virtual ~AudioConvert()
static void error(const char *str,...)
Definition: vbi.cpp:42
static int toFloat8(float *out, const uchar *in, int len)
static av_always_inline av_const long int lrintf(float x)
static uint64_t samples[4]
Definition: element.cpp:45
static int fromFloat16(short *out, const float *in, int len)
void DeinterleaveSamples(int channels, uint8_t *output, const uint8_t *input, int data_size)
AVSampleFormat m_out
#define LOC
AudioConvertInternal * m_ctx
Definition: audioconvert.h:81
int Process(void *out, const void *in, int bytes, bool noclip=false)
Process Parameters: out : destination buffer where converted samples will be copied in : source buffe...
static void MonoToStereo(void *dst, const void *src, int samples)
Convert a mono stream to stereo by copying and interleaving samples.
static int FormatToBits(AudioFormat format)
static int toFloat32(AudioFormat format, float *out, const int *in, int len)
void tInterleaveSample(AudioDataType *out, const AudioDataType *in, int channels, int frames, const AudioDataType *const *inp=nullptr)
static const uint16_t * d
static int SampleSize(AudioFormat format)
void InterleaveSamples(int channels, uint8_t *output, const uint8_t *const *input, int data_size)
static int toFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert integer samples to floats.
unsigned int uint
Definition: compat.h:140
static int fromFloat8(uchar *out, const float *in, int len)
static int fromFloatFLT(float *out, const float *in, int len)
AudioFormat m_out
Definition: audioconvert.h:82
AudioConvertInternal(AVSampleFormat in, AVSampleFormat out)
static int fromFloat32(AudioFormat format, int *out, const float *in, int len)
static short clip_short(int a)
AudioFormat m_in
Definition: audioconvert.h:82
static int toFloat16(float *out, const short *in, int len)
AVSampleFormat m_in
static float clipcheck(float f)
void tDeinterleaveSample(AudioDataType *out, const AudioDataType *in, int channels, int frames)
static uchar clip_uchar(int a)
static AVSampleFormat FormatToAVSampleFormat(AudioFormat format)
Return AudioFormat closest equivalent to AVSampleFormat Note that FORMAT_S24LSB and FORMAT_S24 have n...
#define output
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23