Repository navigation
ip/ffmpeg fixes - #1444
ip/ffmpeg fixes#1444
Conversation
ihy123
commented
Aug 14, 2025
- (More) accurate seeking. This will probably fix No gapless playback and bad seeking for mka (matroska) file with cue sheet. #1397 (see commit message). This is WIP.
- I will also address Segfault in ip_setup() #1442 later.
- Also some refactoring is needed I think
|
Apparently ffmpeg, because of its architecture, can't accurately seek even in wav files (lol) Here 7 frames (in ffmpeg terminology) were skipped entirely with a part of 8th frame. Seeking was off by 334ms |
|
For .ape it's much worse, off by 5.7 sec: And .mka with Atmos off by 2.7 sec: |
|
Now the segfault doesn't happen because ffmpeg_open() fails to create AVFrame with invalid format, so the issue got auto-solved. I added some checks to ip_open() though. When testing these changes I had an issue with player position going over track duration (like 03:58 / 03:57) but it went away after i merged in my other branches, so it's not caused by this branch |
|
Turns out that the bug with duration is ffmpeg's bug, because mpv has same issue. ffmpeg reports incorrect duration for some files. With flac and mp3 it's correct, but not with mpc, for example. Otherwise I believe this patch is ready. I did some testing and it works |
There was a problem hiding this comment.
No comments except that it looks good to me and a lot easier to follow than before.
Also I don't think it's even related to the skip_samples code, but somewhere in the refactoring it fixed encoder delay not being removed. I'm not even sure why it wasn't being removed before. But anyway this fixes gapless mp3 playback and greatly reduces gaps for m4a.
|
@flyingmutant rebase merge? |
|
@ihy123, thanks for your recent high-quality PRs! |
|
fixes #1446 |
|
Check out the last commit please, @gavtroy |
For me ffmpeg only removes the gap at the beginning of the file, but not at the end. Is ffmpeg to blame, or fdkaac, I don't know. |
av_seek_frame() and avformat_seek_file() seek to nearest "keyframe". For codecs like, for example, ape this means that seeking will be very off (5 seconds or more). So what we do is: 1. seek to nearest "keyframe" before the desired time, 2. discard some frames to approach the desired time.
ffmpeg download page states that v4.0.6 has - libavutil 56.14.100 - libavcodec 58.18.100 - libavformat 58.12.100 (https://ffmpeg.org/olddownload.html) After removing all checks for versions lower than these, the plugin still compiles with v3.3.9 headers. After all, why be better with compatibility than developers themselves?
To prevent segfault in ip_setup() because channels=0, validate ip_data->sf after opening ip.
Previously ffmpeg_read()'s while loop was kinda leaking into ffmpeg_get_frame(), now it doesn't.
avcodec_close() can be safely removed because avcodec_free_context() is its replacement since 2016. See ffmpeg commit 2ef6dab0a79 Builds with v3.3.9 v4.0.6 v6.1.3 v7.1.1 v8.0
Makes sense to me 👍 |
The player part is actually relatively self-contained, and I wouldn't be opposed to it being rewritten (or at least significantly reworked) if you want to do it. I've been meaning to myself for a while. |
|
It has some scary multithreaded code. If I were to rewrite it, I would get rid of threads (from whole project even). Even if cmus had a complex audio processing pipeline (for example, resampling -> equalizer -> dither -> ...) then I'd rather fork() and use pipes. But that's too major of a change which would trigger a huge domino effect. And why change it if it works... I should write my own player... (while listening to cmus in the background) |
I bet threads are a significant barrier actually. You're probably like:
That's why I don't like them. 1000+ lines of single-threaded code would be much better |
|
Are there any popular distributions that ship FFmpeg versions that will break build after 07e4c10? |
|
|
||
| res = avcodec_send_packet(priv->codec_ctx, priv->pkt); | ||
| if (res == AVERROR(EAGAIN)) | ||
| if (res == 0 || res == AVERROR(EAGAIN)) |
There was a problem hiding this comment.
res == 0 means success, how is it related to empty frames?
There was a problem hiding this comment.
Because if avcodec_send_packet() is successful, we have to avcodec_receive_frame() again before going on, otherwise the frame remains unchanged (empty since we call this function - ffmpeg_get_frame() - when we need to fill up the frame)
| *out_sample_fmt = cc->sample_fmt; | ||
| switch (*out_sample_fmt) { | ||
| case AV_SAMPLE_FMT_U8: | ||
| sf |= sf_bits(8) | sf_signed(0); |
There was a problem hiding this comment.
u8 would be converted to s16 by cmus anyway, see input.c line 70:
/*
* pcm is converted to 16-bit signed little-endian stereo
* NOTE: no conversion is done if channels > 2 or bits > 16
*/There was a problem hiding this comment.
also s16 is better resolution for applying replaygain and volume, there will be less distortion
|
|
I have just tried it out and I can confirm that this indeed fixes #1397. |
https://build.opensuse.org/request/show/1310390 by user mia + dimstar_suse - Add ffmpeg-8-compat.patch gh#cmus/cmus#1444
https://build.opensuse.org/request/show/1394501 by user mia + anag_factory - Update to 2.13.0 * Set the minimum requested AAudio buffer capacity gh#cmus/cmus#1386 * Check for libavutil pkg-config gh#cmus/cmus#1388 * Add sort by duration gh#cmus/cmus#1440 * Respect scroll_offset setting when resizing window gh#cmus/cmus#1413 * Try to fallback to any plugin upon failure to init output plugin gh#cmus/cmus#1416 * PulseAudio bugfixes and APPLICATION_ID gh#cmus/cmus#1405 * ip/ffmpeg fixes and ffmpeg 8.0 support gh#cmus/cmus#1444 * CUE handling improvements and replaygain/genre support gh#cmus/cmus#1406 * Fix for missing Icecast streaming bitrate display. gh#cmus/cmus#1451 * op/mixer_alsa: alsamixer's volume scale; setting mixer control like aplay does it gh#cmus/cmus#1445 * ip/ffmpeg: fix seeking and