Commit Graph

1959 Commits

Author SHA1 Message Date
ferreum 773c5e2ae0 af_scaletempo2: migrate to internals to talloc
Fixes corrupted audio after resize_input_buffer; realloc_2d did not move
data to new location. Rather than reimplementing more allocator logic,
migrate internals to use talloc and grow buffer with realloc.
2024-04-27 01:05:07 +02:00
ferreum 190b15c827 af_scaletempo2: remove redundant buffer zeroing
First iteration does not overlap with initial buffer contents any more,
so this zeroing was redundant.
2024-04-27 01:05:07 +02:00
nanahi 51e01e9772 ao_wasapi: fix player core lockup when avoiding premature buffer fills
6863eefc3d handled this situation by using
an atomic variable to express the state for which the wakeup is caused
by AO control, and the dispatch queue is only processed at this state.
However, this can cause permanent lockup of the player core when the
following happens:

- AO control sets the thread state to WASAPI_THREAD_DISPATCH, and
  sets the wakeup handle.
- WASAPI thread reads the WASAPI_THREAD_DISPATCH state and processes
  the dispatch queue.
- Another AO control happens. A dispatch item is enqueued, and the
  state stays at WASAPI_THREAD_DISPATCH.
- WASAPI thread resets the thread state to WASAPI_THREAD_FEED since
  the state has not changed.
- WaitForSingleObject() returns in the WASAPI thread, sees this state,
  and does not process the dispatch queue.
- The player core locks permanently because it is waiting for the dispatch
  to be processed.

This has been experimentally verified on a system under high contention:
The easiest way to trigger this lockup is to continuously hold down "i",
which rapidly issues AO get volume/mute controls.

To properly handle this, use separate handles for system and user wakeup
requests. Only feed audio when woke up by system and only process the
dispatch queue when woke up by user.

Fixes: 6863eefc3d
2024-04-27 00:59:09 +02:00
nanahi 7f0961479a Revert "ao_wasapi: address premature buffer fills in exclusive mode"
This reverts commit 6863eefc3d.
2024-04-27 00:59:09 +02:00
Robert Kopaczewski e7b0d6b38b ao/avfoundation: optimise preprocessors for included coreaudio code 2024-04-20 00:44:46 +02:00
Robert Kopaczewski 578b9dade2 ao/audiounit: fix building for iOS 2024-04-20 00:44:46 +02:00
Misaki Kasumi e855836ed1 ao_coreaudio: add a comment for ignoring returned sample count
Co-authored-by: sfan5 <sfan5@live.de>
2024-04-20 00:12:16 +02:00
Misaki Kasumi d46d428f73 Revert "ao_coreaudio: signal buffer underruns"
This reverts commit 0341a6f1d3.
Fixes #13348.
2024-04-20 00:12:16 +02:00
sunpenghao f75f32977c ao_wasapi: set 0 buffer duration on initialization for shared mode
Microsoft requires that both `hnsBufferDuration` and `hnsPeriodicity` should be
0 when initializing a shared mode stream using event-driven buffering. Do as
they say.

Ref: https://learn.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient-initialize
2024-04-19 02:28:23 +02:00
sunpenghao 503a0f184c ao_wasapi: add `--wasapi-exclusive-buffer` option
This allows users to set buffer duration in exclusive mode. We have
been using the default device period as the buffer size and it is
robust enough in most cases. However, on some devices there are
horrible glitches after a stream reset. Unfortunately, the issue is not
consistently reproducible, but using a smaller buffer size (e.g., the
minimum device period) seems to resolve the problem.

Fixes #13715.
2024-04-19 02:28:23 +02:00
m154k1 7a8a92be8d Revert "ao_coreaudio: switch to ao_read_data_nonblocking()"
This reverts commit 36d5b52612.
2024-04-17 21:04:34 +02:00
Kacper Michajłow e720159f72 player/command: add video-codec-info and audio-codec-info
Adds support for extracting codec profile. Old properties are redirected
to new one and removed from docs. Likely will stay like that forever as
there is no reason to remove them.

As a effect of unification of properties between audio and video,
video-codec will now print codec (format) descriptive name, not decoder
long name as it were before. In practice this change fixes what docs
says. If you really need decoder name, use the `track-list/N/decoder-desc`.
2024-04-15 19:34:40 +02:00
ferreum 096d35dac7 af_scaletempo2: prioritize louder channels for similarity measure
Playback with many audio channels could be distorted when using
scaletempo2. This was most noticeable when there were a lot of quiet
channels and few louder channels.

Fix this by increasing the weight of louder channels in relation to
quieter channels. Each channel's target block energy is factored into
the usual similarity measure.

This should have little effect on very correlated channels (such as most
stereo media), where the factors are very similar for all channels.

See-Also: #8705
See-Also: #13737
2024-04-12 17:40:00 +00:00
nanahi 9bb7d96bf9 various: make filter internal function names more descriptive
Lots of filters have generic internal function names like "process".
On a stack trace, all of the different filters use this name,
which causes confusion of the actual filter being processed.

This renames these internal function names to carry the filter names.
This matches what had already been done for some filters.
2024-04-10 19:00:22 +02:00
nanahi 06f88dfb3a ao: rename playthread to ao_thread
"playthread" is a confusing name which doesn't describe what it really
is. Rename it to ao_thread, and ao_wakeup_playthread to ao_wakeup,
in the same style as VO threads. This makes call stack function names
less confusing.
2024-04-10 19:00:22 +02:00
Misaki Kasumi f974382ca0 ao_pipewire: fix delay calculation
A figure from pipewire documentation:

```
           stream time domain           graph time domain
         /-----------------------\/-----------------------------\

 queue     +-+ +-+  +-----------+                 +--------+
 ---->     | | | |->| converter | ->   graph  ->  | kernel | -> speaker
 <----     +-+ +-+  +-----------+                 +--------+
 dequeue   buffers                \-------------------/\--------/
                                     graph              internal
                                    latency             latency
         \--------/\-------------/\-----------------------------/
           queued      buffered            delay
```

We calculate `end_time` in the following steps:

1. get current timestamp in mpv
```
int64_t end_time = mp_time_ns();
```

2. add duration of samples to enqueue
```
end_time += MP_TIME_S_TO_NS(nframes) / ao->samplerate;
```

3. add delay of the pipewire graph
```
end_time += MP_TIME_S_TO_NS(time.delay) * time.rate.num / time.rate.denom;
```

4. add duration of queued and buffered samples.
```
end_time += MP_TIME_S_TO_NS(time.queued) / ao->samplerate;
end_time += MP_TIME_S_TO_NS(time.buffered) / ao->samplerate;
```
New in this commit. `time.queued` is usually zero as `SPA_PARAM_BUFFERS_buffers`
is default to 1; however it is not always.
`time.buffered` is non-zero if there is a resampler involved.

5. add elapsed duration from when `time` is captured
```
end_time -= pw_stream_get_nsec(p->stream) - time.now;
```
New in this commit. `time` is captured at `time.now`.
From then, time has passed so we need to exclude the elapsed time,
by calculating the diff of `pw_stream_get_nsec()` and `time.now`.
2024-04-05 17:22:17 +02:00
Jan Ekström fef04315a1 audio/ad_spdif: utilize defined freeing function for AVIOContext
This has been around since FFmpeg/FFmpeg@b12e4d3bb8
from 2017. Thanks to @mkver for noticing this.
2024-04-04 17:03:48 +03:00
Jan Ekström 951153e733 audio/ad_spdif: specify media type and sample rate in output codecpar
No idea how things previously worked without having these set, but
apparently they did...

If this was a normal encoder to muxer case, we would utilize
`avcodec_parameters_to_context`, but alas this is not.

Fixes: #13794
2024-04-04 17:03:48 +03:00
Misaki Kasumi 4ce4bf1795 ao_coreaudio: register hotplug_cb in normal init() as well
`hotplug_cb` was registered only in `hotplug_init()`.
This commit make it registered in `init()` as well,
so that the ao can listen for latency change
in playback.
2024-04-03 23:43:24 +02:00
Misaki Kasumi 2407e1b2d0 ao_pipewire: support set_pause 2024-04-03 23:40:05 +02:00
Misaki Kasumi d419cc562d ao_wasapi: support set_pause 2024-04-03 23:40:05 +02:00
Misaki Kasumi dbc1e3a459 ao_avfoundation: support set_pause 2024-04-03 23:40:05 +02:00
Misaki Kasumi 93a924a553 ao: set_pause for pull based ao 2024-04-03 23:40:05 +02:00
Misaki Kasumi 7f3ca6c524 ao_pipewire: fix buffer size calculation
`ao->sstride` is alrady initialized to the same value in `init()`
but in addition it can also handle planar formats.
2024-03-31 12:57:52 +02:00
Misaki Kasumi 3086f8fa3e ao_pipewire: fix nframes calculation
`buf` contains a `struct spa_data` for each channel.
Therefore the number of channels does not matter to calculate the frame capacity of one `struct spa_data`.
In practice this shouldn't make a difference as `b->requested` would reduce nframes even more.
2024-03-31 12:57:52 +02:00
nanahi 765a43a0ff ao_alsa: fix snd_config memory leak
During AO init, snd_pcm_open() is called, which calls snd_config_update()
to allocate a global config node and stores it in the snd_config global
variable. This is never freed on uninit.

Fix this by freeing the global config node on uninit.
2024-03-30 10:09:37 +01:00
Misaki Kasumi 276bbb8884 ao_coreaudio: handle latency change on hotplug
The device latency may change during hotplugging.
This commit updates p->hw_latency_ns each time
hotplug_cb is called so that it can reflect
updated device latency.
2024-03-29 14:03:24 +01:00
Misaki Kasumi 1ed8607292 ao_avfoundation: initial avfoundation ao support 2024-03-29 13:46:59 +01:00
nanahi 7ab1080749 af_scaletempo2: fix false reporting of frame availability
With certain speed settings, the following can happen at the start of
the playback:

- can_perform_wsola returns false, so no frames are written
- mp_scaletempo2_frames_available returns true when
  p->input_buffer_final_frames is 0 and target_block_index < 0

This results in infinite loop and completely stalls audio filter
processing and playback. Fix this by only checking this condition
after the final frame is set.

Fixes: 8080d00d7f
2024-03-28 16:16:43 +01:00
sfan5 8e3737ab63 ao_pulse: reenable latency hacks by default
As far as I can tell PulseAudio introduced a bug in 16.0
where if a stream is (un)paused too often the reported latency
will momentarily spike by 3000% or more. Apparently in certain cases
just pausing once and waiting can also cause this.

Save the remaining users of PA the trouble of debugging the various
obscure issues that can arise from this (desync is a harmless example)
by enabling the latency hack code again.

ref: <https://github.com/mpv-player/mpv/issues/12057>
     <https://github.com/mpv-player/mpv/issues/10333>
2024-03-24 09:58:41 +01:00
mistraid121 574f269d32 af_lavcac3enc: fix memory leak on 2ch audio
If processing is not required, the frame would be leaked as it is not used.
2024-03-19 19:32:55 +01:00
nanahi 5fea0f9a47 various: use thread safe mp_strerror() 2024-03-19 19:30:27 +01:00
nanahi e9f966595c ao_lavc: fix warning: ISO C forbids forward references to 'enum' types 2024-03-19 08:58:18 +01:00
nanahi 82a186567e various: fix -Wold-style-declaration warning
warning: `static' is not at beginning of declaration
2024-03-19 08:58:18 +01:00
sfan5 ead9f892b3 various: use static assertions where appropriate 2024-03-17 20:04:04 +01:00
Vilius ab419a6660 ao_coreaudio: stop audio unit after idle timeout
Commit 39f7f83 changed ao_driver.reset to use AudioUnitReset instead of
AudioOutputUnitStop. The problem with calling AudioOutputUnitStop was
that AudioOutputUnitStart takes a significant amount of time after a
stop when a wireless audio device is being used. This resulted in
lagging that was noticeable to users during seeking and short
pause/resume cycles. Switching to AudioUnitReset eliminated this
lagging.

However with the switch to AudioUnitReset the macOS daemon coreaudiod
continued to consume CPU time and did not release a powerd assertion
that it created on behalf of mpv, preventing macOS from sleeping.

This commit will change ao_coreaudio.reset to call AudioOutputUnitStop
after a delay if playback has not resumed. This preserves the faster
restart of playback for seeking and short pause/resume cycles and avoids
preventing sleep and needless CPU consumption.

Fixes #11617

The code changes were authored by @orion1vi and @lhc70000.

Co-authored-by: Collider LI <lhc199652@gmail.com>
2024-03-16 15:00:46 +01:00
Alex Mitzsch 1bf821ebdc ad_spdif: update deprecated FF_PROFILE_DTS_HD_HRA definition
One deprecated FF_PROFILE_DTS_HD_HRA definition was left unaltered - fix that.
2024-03-10 20:59:20 +01:00
Dudemanguy 62b1bad755 ad_spdif: handle const buf pointee in avio_alloc_context
ffmpeg recently changed this field to be const which causes our CI to
fail on newer versions.

See: 2a68d945cd
2024-03-07 22:03:55 +00:00
Dudemanguy 83bad548d2 ad_spdif: handle deprecated FF_PROFILE_* definitions
See: 8238bc0b5e
2024-03-05 19:04:11 +01:00
sfan5 d955dfab29 misc/jni: reduce duplication in mapping struct
'name' was in fact unused when reading fields or methods, so it can be merged with 'method'.
Also changed the type of 'mandatory' to bool.
2024-02-28 16:11:54 +01:00
sfan5 5b1eaf3ff1 misc/jni: introduce macros for deleting references 2024-02-28 16:11:54 +01:00
sfan5 1f3758adea ao_audiotrack: refactor JNI class retrieval
- split mapping from field struct
- mark field struct static
- define list of classes to reduce more repetitive code
2024-02-28 16:11:54 +01:00
sfan5 87d30899ff ao_audiotrack: remove two dead variables 2024-02-28 16:11:54 +01:00
sfan5 3c1c848c2b ao_audiotrack: fix missing check for passthrough support 2024-02-28 16:11:54 +01:00
der richter 86fa9b18a3 osdep/mac: make mac naming of files, folders and function consistent
rename all macOS namings (osx, macosx, macOS, macos, apple) to mac, to
make naming consistent.
2024-02-28 15:52:47 +01:00
nanahi 2872e23aea ao: don't clip floating point formats at non-unity gain
Currently, the softvol gain control attempts to clip floating point ao
formats within -1 and +1. However, this is "optimized out" at unity gain,
where no clipping is applied. This results in inconsistent behavior when
the source audio is already out of -1 and +1 range, where a gain of 0.99
results in clipping, but not at exactly 1.

Since a big advantage of floating point audio data is that they do not
lose information through out-of-range data because the ao sink can apply
suitable negative gain to prevent clipping before converting them to
integer formats, clipping should not be performed on these data.

Fix this by removing the existing clipping behavior. It now results in
a simple multiplication, which faciliates compiler auto-vectorization
of this operation over audio data.
2024-02-25 18:23:57 +00:00
sunpenghao 2cc3bc12db ao_wasapi: scale queried AO volume to (0, 100)
This was done for `AOCONTROL_SET_VOLUME` but not `AOCONTROL_GET_VOLUME`.
2024-02-24 05:26:56 +00:00
sunpenghao 6863eefc3d ao_wasapi: address premature buffer fills in exclusive mode
Currently, running AO control wakes up the WASAPI renderer thread in the
`WASAPI_THREAD_FEED` state, where `thread_feed` will be called. However,
it seems that in recent Windows versions (tested on Windows 10 build
19044.3930 and Windows 11 build 22631.3007) we can't know if it is safe
to feed more audio data in event-driven exclusive mode:
- `IAudioClient_GetCurrentPadding` always returns `bufferFrameCount`,
  even if *NO* data has ever been written. This means we don't know how
  much free space we have that is available for writing. This is not the
  case in shared mode, where the return value correctly reflects the
  size of data waiting to be processed. As a sidenote, MS did not
  document the precise definition of the return value for an
  event-driven, exclusive stream [1].
- `IAudioRenderClient_GetBuffer` never fails. We can call it for 10
  times in a roll, each time requesting an entire buffer (the unit at
  which data is exchanged in exclusive mode using event-driven
  buffering; there are 2 such buffers) and get a successful return code
  everytime. In shared mode, we get `AUDCLNT_E_BUFFER_TOO_LARGE` if we
  request a buffer larger than that currently available.

As a result, `thread_feed` will always write `bufferFrameCount` frames
of audio in exclusive mode. There will therefore be glitches each time
`thread_control` is called due to the subsequent `thread_feed`
overwriting frames yet to be processed. Also, an irreversible error is
accumulated to `sample_count` as long as there is no AO reset, leading
to eventual, unbounded A/V desync.

As a fix to the issue, add a dedicated state for dispatch queue
processing so that `thread_feed` is only called when signaled by the OS.
The buffer checks in `thread_feed` that use `GetCurrentPadding` in
exclusive mode are kept in case there are older versions where the two
APIs behave differently.

Closes #12615.

[1] https://learn.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient-getcurrentpadding
2024-02-24 05:26:56 +00:00
der richter d954646d29 various: make mentions of macOS consistent
change all mentions and variations of OSX, OS X, MacOSX, MacOS X, etc
consistent. use the official naming macOS.
2024-02-21 20:46:53 +01:00
llyyr 6a5a3ec3bf af_lavcac3enc: don't use deprecated `avcodec_close`
Deprecated upstream 1cc24d7495

We need to reallocate the context here because `avcodec_free_context`
also frees the context, and we want to reuse the context with some
reconfig.
2024-02-19 18:09:58 +01:00