MythTV  master
audiooutpututil.cpp
Go to the documentation of this file.
1 #include <cinttypes>
2 #include <cmath>
3 #include <sys/types.h>
4 
5 #include "mythconfig.h"
6 #include "mythlogging.h"
7 #include "audiooutpututil.h"
8 #include "audioconvert.h"
9 #include "bswap.h"
10 #include "libmythtv/mythavutil.h"
11 
12 extern "C" {
13 #include "libavcodec/avcodec.h"
14 #include "pink.h"
15 }
16 
17 #define LOC QString("AOUtil: ")
18 
19 #define ISALIGN(x) (((unsigned long)(x) & 0xf) == 0)
20 
21 #if ARCH_X86
22 static int has_sse2 = -1;
23 
24 // Check cpuid for SSE2 support on x86 / x86_64
25 static inline bool sse_check()
26 {
27  if (has_sse2 != -1)
28  return (bool)has_sse2;
29  __asm__(
30  // -fPIC - we may not clobber ebx/rbx
31 #if ARCH_X86_64
32  "push %%rbx \n\t"
33 #else
34  "push %%ebx \n\t"
35 #endif
36  "mov $1, %%eax \n\t"
37  "cpuid \n\t"
38  "and $0x4000000, %%edx \n\t"
39  "shr $26, %%edx \n\t"
40 #if ARCH_X86_64
41  "pop %%rbx \n\t"
42 #else
43  "pop %%ebx \n\t"
44 #endif
45  :"=d"(has_sse2)
46  ::"%eax","%ecx"
47  );
48  return (bool)has_sse2;
49 }
50 #endif //ARCH_x86
51 
57 {
58 #if ARCH_X86
59  return sse_check();
60 #else
61  return false;
62 #endif
63 }
64 
70 int AudioOutputUtil::toFloat(AudioFormat format, void *out, const void *in,
71  int bytes)
72 {
73  return AudioConvert::toFloat(format, out, in, bytes);
74 }
75 
81 int AudioOutputUtil::fromFloat(AudioFormat format, void *out, const void *in,
82  int bytes)
83 {
84  return AudioConvert::fromFloat(format, out, in, bytes);
85 }
86 
90 void AudioOutputUtil::MonoToStereo(void *dst, const void *src, int samples)
91 {
93 }
94 
101 void AudioOutputUtil::AdjustVolume(void *buf, int len, int volume,
102  bool music, bool upmix)
103 {
104  float g = volume / 100.0F;
105  float *fptr = (float *)buf;
106  int samples = len >> 2;
107  int i = 0;
108 
109  // Should be exponential - this'll do
110  g *= g;
111 
112  // Try to ~ match stereo volume when upmixing
113  if (upmix)
114  g *= 1.5F;
115 
116  // Music is relatively loud
117  if (music)
118  g *= 0.4F;
119 
120  if (g == 1.0F)
121  return;
122 
123 #if ARCH_X86
124  if (sse_check() && samples >= 16)
125  {
126  int loops = samples >> 4;
127  i = loops << 4;
128 
129  __asm__ volatile (
130  "movss %2, %%xmm0 \n\t"
131  "punpckldq %%xmm0, %%xmm0 \n\t"
132  "punpckldq %%xmm0, %%xmm0 \n\t"
133  "1: \n\t"
134  "movups (%0), %%xmm1 \n\t"
135  "movups 16(%0), %%xmm2 \n\t"
136  "mulps %%xmm0, %%xmm1 \n\t"
137  "movups 32(%0), %%xmm3 \n\t"
138  "mulps %%xmm0, %%xmm2 \n\t"
139  "movups 48(%0), %%xmm4 \n\t"
140  "mulps %%xmm0, %%xmm3 \n\t"
141  "movups %%xmm1, (%0) \n\t"
142  "mulps %%xmm0, %%xmm4 \n\t"
143  "movups %%xmm2, 16(%0) \n\t"
144  "movups %%xmm3, 32(%0) \n\t"
145  "movups %%xmm4, 48(%0) \n\t"
146  "add $64, %0 \n\t"
147  "sub $1, %%ecx \n\t"
148  "jnz 1b \n\t"
149  :"+r"(fptr)
150  :"c"(loops),"m"(g)
151  );
152  }
153 #endif //ARCH_X86
154  for (; i < samples; i++)
155  *fptr++ *= g;
156 }
157 
158 template <class AudioDataType>
159 void _MuteChannel(AudioDataType *buffer, int channels, int ch, int frames)
160 {
161  AudioDataType *s1 = buffer + ch;
162  AudioDataType *s2 = buffer - ch + 1;
163 
164  for (int i = 0; i < frames; i++)
165  {
166  *s1 = *s2;
167  s1 += channels;
168  s2 += channels;
169  }
170 }
171 
178 void AudioOutputUtil::MuteChannel(int obits, int channels, int ch,
179  void *buffer, int bytes)
180 {
181  int frames = bytes / ((obits >> 3) * channels);
182 
183  if (obits == 8)
184  _MuteChannel((uchar *)buffer, channels, ch, frames);
185  else if (obits == 16)
186  _MuteChannel((short *)buffer, channels, ch, frames);
187  else
188  _MuteChannel((int *)buffer, channels, ch, frames);
189 }
190 
191 #if HAVE_BIGENDIAN
192 #define LE_SHORT(v) bswap_16(v)
193 #define LE_INT(v) bswap_32(v)
194 #else
195 #define LE_SHORT(v) (v)
196 #define LE_INT(v) (v)
197 #endif
198 
199 char *AudioOutputUtil::GeneratePinkFrames(char *frames, int channels,
200  int channel, int count, int bits)
201 {
202  pink_noise_t pink;
203 
204  initialize_pink_noise(&pink, bits);
205 
206  double res;
207  int32_t ires;
208  int16_t *samp16 = (int16_t*) frames;
209  int32_t *samp32 = (int32_t*) frames;
210 
211  while (count-- > 0)
212  {
213  for(int chn = 0 ; chn < channels; chn++)
214  {
215  if (chn==channel)
216  {
217  res = generate_pink_noise_sample(&pink) * 0x03fffffff; /* Don't use MAX volume */
218  ires = res;
219  if (bits == 16)
220  *samp16++ = LE_SHORT(ires >> 16);
221  else
222  *samp32++ = LE_INT(ires);
223  }
224  else
225  {
226  if (bits == 16)
227  *samp16++ = 0;
228  else
229  *samp32++ = 0;
230  }
231  }
232  }
233  return frames;
234 }
235 
243 int AudioOutputUtil::DecodeAudio(AVCodecContext *ctx,
244  uint8_t *buffer, int &data_size,
245  const AVPacket *pkt)
246 {
247  MythAVFrame frame;
248  bool got_frame = false;
249  int ret;
250  char error[AV_ERROR_MAX_STRING_SIZE];
251 
252  data_size = 0;
253  if (!frame)
254  {
255  return AVERROR(ENOMEM);
256  }
257 
258 // SUGGESTION
259 // Now that avcodec_decode_audio4 is deprecated and replaced
260 // by 2 calls (receive frame and send packet), this could be optimized
261 // into separate routines or separate threads.
262 // Also now that it always consumes a whole buffer some code
263 // in the caller may be able to be optimized.
264  ret = avcodec_receive_frame(ctx,frame);
265  if (ret == 0)
266  got_frame = true;
267  if (ret == AVERROR(EAGAIN))
268  ret = 0;
269  if (ret == 0)
270  ret = avcodec_send_packet(ctx, pkt);
271  if (ret == AVERROR(EAGAIN))
272  ret = 0;
273  else if (ret < 0)
274  {
275  LOG(VB_AUDIO, LOG_ERR, LOC +
276  QString("audio decode error: %1 (%2)")
277  .arg(av_make_error_string(error, sizeof(error), ret))
278  .arg(got_frame));
279  return ret;
280  }
281  else
282  ret = pkt->size;
283 
284  if (!got_frame)
285  {
286  LOG(VB_AUDIO, LOG_DEBUG, LOC +
287  QString("audio decode, no frame decoded (%1)").arg(ret));
288  return ret;
289  }
290 
291  AVSampleFormat format = (AVSampleFormat)frame->format;
292 
293  data_size = frame->nb_samples * frame->channels * av_get_bytes_per_sample(format);
294 
295  if (av_sample_fmt_is_planar(format))
296  {
297  InterleaveSamples(AudioOutputSettings::AVSampleFormatToFormat(format, ctx->bits_per_raw_sample),
298  frame->channels, buffer, (const uint8_t **)frame->extended_data,
299  data_size);
300  }
301  else
302  {
303  // data is already compacted... simply copy it
304  memcpy(buffer, frame->extended_data[0], data_size);
305  }
306 
307  return ret;
308 }
309 
315  uint8_t *output, const uint8_t *input,
316  int data_size)
317 {
318  AudioConvert::DeinterleaveSamples(format, channels, output, input, data_size);
319 }
320 
327  uint8_t *output, const uint8_t * const *input,
328  int data_size)
329 {
330  AudioConvert::InterleaveSamples(format, channels, output, input, data_size);
331 }
332 
338  uint8_t *output, const uint8_t *input,
339  int data_size)
340 {
341  AudioConvert::InterleaveSamples(format, channels, output, input, data_size);
342 }
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
static int toFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert integer samples to floats.
static uint64_t samples[4]
Definition: element.c:45
void DeinterleaveSamples(int channels, uint8_t *output, const uint8_t *input, int data_size)
static void error(const char *str,...)
Definition: vbi.c:42
static void MonoToStereo(void *dst, const void *src, int samples)
Convert a mono stream to stereo by copying and interleaving samples.
static int DecodeAudio(AVCodecContext *ctx, uint8_t *buffer, int &data_size, const AVPacket *pkt)
DecodeAudio Decode an audio packet, and compact it if data is planar Return negative error code if an...
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
float generate_pink_noise_sample(pink_noise_t *pink)
Definition: pink.c:56
void _MuteChannel(AudioDataType *buffer, int channels, int ch, int frames)
static void InterleaveSamples(AudioFormat format, int channels, uint8_t *output, const uint8_t *const *input, int data_size)
Interleave input samples Planar audio is contained in array of pointers Interleave audio samples (con...
static char * GeneratePinkFrames(char *frames, int channels, int channel, int count, int bits=16)
#define LE_INT(v)
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.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
#define LOC
#define LE_SHORT(v)
static void AdjustVolume(void *buffer, int len, int volume, bool music, bool upmix)
Adjust the volume of samples.
const char * frames[3]
Definition: element.c:46
static bool has_hardware_fpu()
Returns true if platform has an FPU.
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythavutil.h:42
static void MonoToStereo(void *dst, const void *src, int samples)
Convert a mono stream to stereo by copying and interleaving samples.
static void MuteChannel(int obits, int channels, int ch, void *buffer, int bytes)
Mute individual channels through mono->stereo duplication.
static AudioFormat AVSampleFormatToFormat(AVSampleFormat format, int bits=0)
Return AVSampleFormat closest equivalent to AudioFormat.
void initialize_pink_noise(pink_noise_t *pink, int num_rows)
Definition: pink.c:41
static void DeinterleaveSamples(AudioFormat format, int channels, uint8_t *output, const uint8_t *input, int data_size)
Deinterleave input samples Deinterleave audio samples and compact them.
#define output
unsigned char g
Definition: ParseText.cpp:329