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