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 }
15 #include "pink.h"
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  auto *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 tMuteChannel(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  tMuteChannel((uchar *)buffer, channels, ch, frames);
185  else if (obits == 16)
186  tMuteChannel((short *)buffer, channels, ch, frames);
187  else
188  tMuteChannel((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);
205 
206  auto *samp16 = (int16_t*) frames;
207  auto *samp32 = (int32_t*) frames;
208 
209  while (count-- > 0)
210  {
211  for(int chn = 0 ; chn < channels; chn++)
212  {
213  if (chn==channel)
214  {
215  /* Don't use MAX volume */
216  double res = generate_pink_noise_sample(&pink) *
217  static_cast<float>(0x03fffffff);
218  int32_t 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 
250  data_size = 0;
251  if (!frame)
252  {
253  return AVERROR(ENOMEM);
254  }
255 
256 // SUGGESTION
257 // Now that avcodec_decode_audio4 is deprecated and replaced
258 // by 2 calls (receive frame and send packet), this could be optimized
259 // into separate routines or separate threads.
260 // Also now that it always consumes a whole buffer some code
261 // in the caller may be able to be optimized.
262  int ret = avcodec_receive_frame(ctx,frame);
263  if (ret == 0)
264  got_frame = true;
265  if (ret == AVERROR(EAGAIN))
266  ret = 0;
267  if (ret == 0)
268  ret = avcodec_send_packet(ctx, pkt);
269  if (ret == AVERROR(EAGAIN))
270  ret = 0;
271  else if (ret < 0)
272  {
273  std::string error;
274  LOG(VB_AUDIO, LOG_ERR, LOC +
275  QString("audio decode error: %1 (%2)")
277  .arg(got_frame));
278  return ret;
279  }
280  else
281  ret = pkt->size;
282 
283  if (!got_frame)
284  {
285  LOG(VB_AUDIO, LOG_DEBUG, LOC +
286  QString("audio decode, no frame decoded (%1)").arg(ret));
287  return ret;
288  }
289 
290  auto format = (AVSampleFormat)frame->format;
291 
292  data_size = frame->nb_samples * frame->channels * av_get_bytes_per_sample(format);
293 
294  if (av_sample_fmt_is_planar(format))
295  {
296  InterleaveSamples(AudioOutputSettings::AVSampleFormatToFormat(format, ctx->bits_per_raw_sample),
297  frame->channels, buffer, (const uint8_t **)frame->extended_data,
298  data_size);
299  }
300  else
301  {
302  // data is already compacted... simply copy it
303  memcpy(buffer, frame->extended_data[0], data_size);
304  }
305 
306  return ret;
307 }
308 
314  uint8_t *output, const uint8_t *input,
315  int data_size)
316 {
317  AudioConvert::DeinterleaveSamples(format, channels, output, input, data_size);
318 }
319 
326  uint8_t *output, const uint8_t * const *input,
327  int data_size)
328 {
329  AudioConvert::InterleaveSamples(format, channels, output, input, data_size);
330 }
331 
337  uint8_t *output, const uint8_t *input,
338  int data_size)
339 {
340  AudioConvert::InterleaveSamples(format, channels, output, input, data_size);
341 }
AudioConvert::toFloat
static int toFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert integer samples to floats.
Definition: audioconvert.cpp:521
channel
QDomElement channel
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:498
AudioOutputSettings::AVSampleFormatToFormat
static AudioFormat AVSampleFormatToFormat(AVSampleFormat format, int bits=0)
Return AVSampleFormat closest equivalent to AudioFormat.
Definition: audiooutputsettings.cpp:207
pink.h
error
static void error(const char *str,...)
Definition: vbi.cpp:42
samples
static uint64_t samples[4]
Definition: element.cpp:45
audiooutpututil.h
AudioOutputUtil::GeneratePinkFrames
static char * GeneratePinkFrames(char *frames, int channels, int channel, int count, int bits=16)
Definition: audiooutpututil.cpp:199
AudioOutputUtil::toFloat
static int toFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert integer samples to floats.
Definition: audiooutpututil.cpp:70
MythAVFrame
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythavutil.h:44
arg
arg(title).arg(filename).arg(doDelete))
AudioConvert::fromFloat
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
Definition: audioconvert.cpp:551
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
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:90
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:243
mythlogging.h
LE_INT
#define LE_INT(v)
Definition: audiooutpututil.cpp:196
AudioConvert::DeinterleaveSamples
void DeinterleaveSamples(int channels, uint8_t *output, const uint8_t *input, int data_size)
Definition: audioconvert.cpp:869
AudioOutputUtil::has_hardware_fpu
static bool has_hardware_fpu()
Returns true if platform has an FPU.
Definition: audiooutpututil.cpp:56
initialize_pink_noise
void initialize_pink_noise(pink_noise_t *pink, int num_rows)
Definition: pink.cpp:41
LOC
#define LOC
Definition: audiooutpututil.cpp:17
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
tMuteChannel
void tMuteChannel(AudioDataType *buffer, int channels, int ch, int frames)
Definition: audiooutpututil.cpp:159
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:325
musicbrainzngs.compat.bytes
bytes
Definition: compat.py:49
AudioOutputUtil::AdjustVolume
static void AdjustVolume(void *buffer, int len, int volume, bool music, bool upmix)
Adjust the volume of samples.
Definition: audiooutpututil.cpp:101
bswap.h
mythavutil.h
generate_pink_noise_sample
float generate_pink_noise_sample(pink_noise_t *pink)
Definition: pink.cpp:56
pink_noise_t
Definition: pink.h:13
LE_SHORT
#define LE_SHORT(v)
Definition: audiooutpututil.cpp:195
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:313
AudioConvert::InterleaveSamples
void InterleaveSamples(int channels, uint8_t *output, const uint8_t *const *input, int data_size)
Definition: audioconvert.cpp:876
AudioOutputUtil::fromFloat
static int fromFloat(AudioFormat format, void *out, const void *in, int bytes)
Convert float samples to integers.
Definition: audiooutpututil.cpp:81
AudioFormat
AudioFormat
Definition: audiooutputsettings.h:25
output
#define output
Definition: synaesthesia.cpp:221
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:178
av_make_error_stdstring
char * av_make_error_stdstring(std::string &errbuf, int errnum)
Definition: mythaverror.cpp:41
audioconvert.h