MythTV  master
audiooutpututil.cpp
Go to the documentation of this file.
1 #include "audiooutpututil.h"
2 
3 #include <cstdint>
4 #include <limits> // workaround QTBUG-90395
5 
6 #include <QtGlobal>
7 #include <QtEndian>
8 
10 
11 #include "audioconvert.h"
12 #include "mythaverror.h"
13 
14 extern "C" {
15 #include "libavcodec/avcodec.h"
16 }
17 #include "pink.h"
18 
19 #define LOC QString("AOUtil: ")
20 
21 #ifdef Q_PROCESSOR_X86
22 // Check cpuid for SSE2 support on x86 / x86_64
23 static inline bool sse2_check()
24 {
25 #ifdef Q_PROCESSOR_X86_64
26  return true;
27 #else
28  static int has_sse2 = -1;
29  if (has_sse2 != -1)
30  return (bool)has_sse2;
31  __asm__(
32  // -fPIC - we may not clobber ebx/rbx
33  "push %%ebx \n\t"
34  "mov $1, %%eax \n\t"
35  "cpuid \n\t"
36  "and $0x4000000, %%edx \n\t"
37  "shr $26, %%edx \n\t"
38  "pop %%ebx \n\t"
39  :"=d"(has_sse2)
40  ::"%eax","%ecx"
41  );
42  return (bool)has_sse2;
43 #endif
44 }
45 #endif //Q_PROCESSOR_X86
46 
52 {
53 #ifdef Q_PROCESSOR_X86
54  return sse2_check();
55 #else
56  return false;
57 #endif
58 }
59 
65 int AudioOutputUtil::toFloat(AudioFormat format, void *out, const void *in,
66  int bytes)
67 {
68  return AudioConvert::toFloat(format, out, in, bytes);
69 }
70 
76 int AudioOutputUtil::fromFloat(AudioFormat format, void *out, const void *in,
77  int bytes)
78 {
79  return AudioConvert::fromFloat(format, out, in, bytes);
80 }
81 
85 void AudioOutputUtil::MonoToStereo(void *dst, const void *src, int samples)
86 {
88 }
89 
96 void AudioOutputUtil::AdjustVolume(void *buf, int len, int volume,
97  bool music, bool upmix)
98 {
99  float g = volume / 100.0F;
100  auto *fptr = (float *)buf;
101  int samples = len >> 2;
102  int i = 0;
103 
104  // Should be exponential - this'll do
105  g *= g;
106 
107  // Try to ~ match stereo volume when upmixing
108  if (upmix)
109  g *= 1.5F;
110 
111  // Music is relatively loud
112  if (music)
113  g *= 0.4F;
114 
115  if (g == 1.0F)
116  return;
117 
118 #ifdef Q_PROCESSOR_X86
119  if (sse2_check() && samples >= 16)
120  {
121  int loops = samples >> 4;
122  i = loops << 4;
123 
124  __asm__ volatile (
125  "movss %2, %%xmm0 \n\t"
126  "punpckldq %%xmm0, %%xmm0 \n\t"
127  "punpckldq %%xmm0, %%xmm0 \n\t"
128  "1: \n\t"
129  "movups (%0), %%xmm1 \n\t"
130  "movups 16(%0), %%xmm2 \n\t"
131  "mulps %%xmm0, %%xmm1 \n\t"
132  "movups 32(%0), %%xmm3 \n\t"
133  "mulps %%xmm0, %%xmm2 \n\t"
134  "movups 48(%0), %%xmm4 \n\t"
135  "mulps %%xmm0, %%xmm3 \n\t"
136  "movups %%xmm1, (%0) \n\t"
137  "mulps %%xmm0, %%xmm4 \n\t"
138  "movups %%xmm2, 16(%0) \n\t"
139  "movups %%xmm3, 32(%0) \n\t"
140  "movups %%xmm4, 48(%0) \n\t"
141  "add $64, %0 \n\t"
142  "sub $1, %%ecx \n\t"
143  "jnz 1b \n\t"
144  :"+r"(fptr)
145  :"c"(loops),"m"(g)
146  :"xmm0","xmm1","xmm2","xmm3","xmm4"
147  );
148  }
149 #endif //Q_PROCESSOR_X86
150  for (; i < samples; i++)
151  *fptr++ *= g;
152 }
153 
154 template <class AudioDataType>
155 void tMuteChannel(AudioDataType *buffer, int channels, int ch, int frames)
156 {
157  AudioDataType *s1 = buffer + ch;
158  AudioDataType *s2 = buffer - ch + 1;
159 
160  for (int i = 0; i < frames; i++)
161  {
162  *s1 = *s2;
163  s1 += channels;
164  s2 += channels;
165  }
166 }
167 
174 void AudioOutputUtil::MuteChannel(int obits, int channels, int ch,
175  void *buffer, int bytes)
176 {
177  int frames = bytes / ((obits >> 3) * channels);
178 
179  if (obits == 8)
180  tMuteChannel((uint8_t *)buffer, channels, ch, frames);
181  else if (obits == 16)
182  tMuteChannel((short *)buffer, channels, ch, frames);
183  else
184  tMuteChannel((int *)buffer, channels, ch, frames);
185 }
186 
187 char *AudioOutputUtil::GeneratePinkFrames(char *frames, int channels,
188  int channel, int count, int bits)
189 {
190  pink_noise_t pink{};
191 
192  initialize_pink_noise(&pink);
193 
194  auto *samp16 = (int16_t*) frames;
195  auto *samp32 = (int32_t*) frames;
196 
197  while (count-- > 0)
198  {
199  for(int chn = 0 ; chn < channels; chn++)
200  {
201  if (chn==channel)
202  {
203  /* Don't use MAX volume */
204  double res = generate_pink_noise_sample(&pink) *
205  static_cast<float>(0x03fffffff);
206  int32_t ires = res;
207  if (bits == 16)
208  *samp16++ = qToLittleEndian<qint16>(ires >> 16);
209  else
210  *samp32++ = qToLittleEndian<qint32>(ires);
211  }
212  else
213  {
214  if (bits == 16)
215  *samp16++ = 0;
216  else
217  *samp32++ = 0;
218  }
219  }
220  }
221  return frames;
222 }
223 
231 int AudioOutputUtil::DecodeAudio(AVCodecContext *ctx,
232  uint8_t *buffer, int &data_size,
233  const AVPacket *pkt)
234 {
235  MythAVFrame frame;
236  bool got_frame = false;
237 
238  data_size = 0;
239  if (!frame)
240  {
241  return AVERROR(ENOMEM);
242  }
243 
244 // SUGGESTION
245 // Now that avcodec_decode_audio4 is deprecated and replaced
246 // by 2 calls (receive frame and send packet), this could be optimized
247 // into separate routines or separate threads.
248 // Also now that it always consumes a whole buffer some code
249 // in the caller may be able to be optimized.
250  int ret = avcodec_receive_frame(ctx,frame);
251  if (ret == 0)
252  got_frame = true;
253  if (ret == AVERROR(EAGAIN))
254  ret = 0;
255  if (ret == 0)
256  ret = avcodec_send_packet(ctx, pkt);
257  if (ret == AVERROR(EAGAIN))
258  ret = 0;
259  else if (ret < 0)
260  {
261  std::string error;
262  LOG(VB_AUDIO, LOG_ERR, LOC +
263  QString("audio decode error: %1 (%2)")
264  .arg(av_make_error_stdstring(error, ret))
265  .arg(got_frame));
266  return ret;
267  }
268  else
269  ret = pkt->size;
270 
271  if (!got_frame)
272  {
273  LOG(VB_AUDIO, LOG_DEBUG, LOC +
274  QString("audio decode, no frame decoded (%1)").arg(ret));
275  return ret;
276  }
277 
278  auto format = (AVSampleFormat)frame->format;
279 
280  data_size = frame->nb_samples * frame->ch_layout.nb_channels * av_get_bytes_per_sample(format);
281 
282  if (av_sample_fmt_is_planar(format))
283  {
284  InterleaveSamples(AudioOutputSettings::AVSampleFormatToFormat(format, ctx->bits_per_raw_sample),
285  frame->ch_layout.nb_channels, buffer, (const uint8_t **)frame->extended_data,
286  data_size);
287  }
288  else
289  {
290  // data is already compacted... simply copy it
291  memcpy(buffer, frame->extended_data[0], data_size);
292  }
293 
294  return ret;
295 }
296 
302  uint8_t *output, const uint8_t *input,
303  int data_size)
304 {
305  AudioConvert::DeinterleaveSamples(format, channels, output, input, data_size);
306 }
307 
314  uint8_t *output, const uint8_t * const *input,
315  int data_size)
316 {
317  AudioConvert::InterleaveSamples(format, channels, output, input, data_size);
318 }
319 
325  uint8_t *output, const uint8_t *input,
326  int data_size)
327 {
328  AudioConvert::InterleaveSamples(format, channels, output, input, data_size);
329 }
AudioConvert::toFloat
static int toFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert integer samples to floats.
Definition: audioconvert.cpp:522
AudioOutputSettings::AVSampleFormatToFormat
static AudioFormat AVSampleFormatToFormat(AVSampleFormat format, int bits=0)
Return AVSampleFormat closest equivalent to AudioFormat.
Definition: audiooutputsettings.cpp:198
pink.h
error
static void error(const char *str,...)
Definition: vbi.cpp:36
audiooutpututil.h
AudioOutputUtil::GeneratePinkFrames
static char * GeneratePinkFrames(char *frames, int channels, int channel, int count, int bits=16)
Definition: audiooutpututil.cpp:187
AudioOutputUtil::toFloat
static int toFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert integer samples to floats.
Definition: audiooutpututil.cpp:65
MythAVFrame
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythaverror.h:52
AudioConvert::fromFloat
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
Definition: audioconvert.cpp:552
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
AudioOutputUtil::MonoToStereo
static void MonoToStereo(void *dst, const void *src, int samples)
Convert a mono stream to stereo by copying and interleaving samples.
Definition: audiooutpututil.cpp:85
AudioOutputUtil::DecodeAudio
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...
Definition: audiooutpututil.cpp:231
mythlogging.h
AudioConvert::DeinterleaveSamples
void DeinterleaveSamples(int channels, uint8_t *output, const uint8_t *input, int data_size)
Definition: audioconvert.cpp:876
initialize_pink_noise
void initialize_pink_noise(pink_noise_t *pink, int num_rows)
Definition: pink.cpp:41
LOC
#define LOC
Definition: audiooutpututil.cpp:19
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:728
tMuteChannel
void tMuteChannel(AudioDataType *buffer, int channels, int ch, int frames)
Definition: audiooutpututil.cpp:155
AudioOutputUtil::InterleaveSamples
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...
Definition: audiooutpututil.cpp:313
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
AudioOutputUtil::has_optimized_SIMD
static bool has_optimized_SIMD()
Returns true if the processor supports MythTV's optimized SIMD for AudioOutputUtil/AudioConvert.
Definition: audiooutpututil.cpp:51
AudioOutputUtil::AdjustVolume
static void AdjustVolume(void *buffer, int len, int volume, bool music, bool upmix)
Adjust the volume of samples.
Definition: audiooutpututil.cpp:96
generate_pink_noise_sample
float generate_pink_noise_sample(pink_noise_t *pink)
Definition: pink.cpp:56
pink_noise_t
Definition: pink.h:12
AudioOutputUtil::DeinterleaveSamples
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.
Definition: audiooutpututil.cpp:301
AudioConvert::InterleaveSamples
void InterleaveSamples(int channels, uint8_t *output, const uint8_t *const *input, int data_size)
Definition: audioconvert.cpp:883
AudioOutputUtil::fromFloat
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
Definition: audiooutpututil.cpp:76
AudioFormat
AudioFormat
Definition: audiooutputsettings.h:24
output
#define output
Definition: synaesthesia.cpp:220
samples
static const std::array< const uint64_t, 4 > samples
Definition: element.cpp:46
AudioOutputUtil::MuteChannel
static void MuteChannel(int obits, int channels, int ch, void *buffer, int bytes)
Mute individual channels through mono->stereo duplication.
Definition: audiooutpututil.cpp:174
mythaverror.h
av_make_error_stdstring
char * av_make_error_stdstring(std::string &errbuf, int errnum)
Definition: mythaverror.cpp:41
audioconvert.h