Skip to content

ip/ffmpeg fixes - #1444

Merged
flyingmutant merged 13 commits into
cmus:masterfrom
ihy123:ffmpeg
Sep 2, 2025
Merged

flyingmutant merged 13 commits into
cmus:masterfrom
ihy123:ffmpeg

Conversation

@ihy123

@ihy123 ihy123 commented Aug 14, 2025

Copy link
Copy Markdown
Contributor
  1. (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.
  2. I will also address Segfault in ip_setup() #1442 later.
  3. Also some refactoring is needed I think

@ihy123

ihy123 commented Aug 15, 2025

Copy link
Copy Markdown
Contributor Author

Apparently ffmpeg, because of its architecture, can't accurately seek even in wav files (lol)
Divide numbers by 10^6 to get seconds:

player_seek: doing op_drop after seek
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12000000, frame_end: 12042667
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12042667, frame_end: 12085334
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12085333, frame_end: 12128000
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12128000, frame_end: 12170667
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12170667, frame_end: 12213334
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12213333, frame_end: 12256000
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12256000, frame_end: 12298667
ffmpeg_fill_buffer: seek_ts: 12334666, frame_ts: 12298667, frame_end: 12341334
ffmpeg_fill_buffer: skipping 1728 samples

Here 7 frames (in ffmpeg terminology) were skipped entirely with a part of 8th frame. Seeking was off by 334ms

@ihy123

ihy123 commented Aug 15, 2025 •

Copy link
Copy Markdown
Contributor Author

For .ape it's much worse, off by 5.7 sec:

ffmpeg_fill_buffer: seek_ts: 727960000, frame_ts: 722233469, frame_end: 722337959
... (53 more lines) ...
ffmpeg_fill_buffer: seek_ts: 727960000, frame_ts: 727875929, frame_end: 727980419
ffmpeg_fill_buffer: skipping 3708 samples

And .mka with Atmos

> ffprobe test.mka
Input #0, matroska,webm, from 'test.mka':
  Metadata:
    ENCODER         : Lavf60.16.100
  Duration: 00:05:00.00, start: 0.000000, bitrate: 4518 kb/s
  Stream #0:0: Audio: truehd (Dolby TrueHD + Dolby Atmos), 48000 Hz, 7.1, s32 (24 bit)
    Metadata:
      DURATION        : 00:05:00.000000000

off by 2.7 sec:

ffmpeg_fill_buffer: seek_ts: 82741332, frame_ts: 80064000, frame_end: 80064833
... (3211 more lines) ...
ffmpeg_fill_buffer: seek_ts: 82741332, frame_ts: 82741000, frame_end: 82741833
ffmpeg_fill_buffer: skipping 16 samples

@ihy123
ihy123 marked this pull request as ready for review August 17, 2025 13:09
@ihy123

ihy123 commented Aug 17, 2025

Copy link
Copy Markdown
Contributor Author

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

@ihy123

ihy123 commented Aug 17, 2025

Copy link
Copy Markdown
Contributor Author

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.
For one of my .mpc files mpcdec (original decoder) tells 10:20, but ffprobe tells 10:19.

Otherwise I believe this patch is ready. I did some testing and it works

@pgaskin
pgaskin requested a review from gavtroy August 17, 2025 23:50

@gavtroy gavtroy left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pgaskin
pgaskin requested review from flyingmutant and removed request for flyingmutant August 20, 2025 14:30
@pgaskin

pgaskin commented Aug 20, 2025

Copy link
Copy Markdown
Member

@flyingmutant rebase merge?

@pgaskin

pgaskin commented Aug 20, 2025

Copy link
Copy Markdown
Member

@ihy123, thanks for your recent high-quality PRs!

@pgaskin

pgaskin commented Aug 24, 2025

Copy link
Copy Markdown
Member

fixes #1446

@ihy123

ihy123 commented Aug 25, 2025

Copy link
Copy Markdown
Contributor Author

Check out the last commit please, @gavtroy
It would've been even better to never convert floats to ints (or convert them after applying replaygain), but I'd like to avoid rewriting the whole player :)

@ihy123

ihy123 commented Aug 25, 2025

Copy link
Copy Markdown
Contributor Author

But anyway this fixes gapless mp3 playback and greatly reduces gaps for m4a.

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.

ihy123 added 11 commits August 25, 2025 08:43
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.
ihy123 added 2 commits August 25, 2025 08:43
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
@gavtroy

gavtroy commented Aug 25, 2025

Copy link
Copy Markdown
Collaborator

Check out the last commit please

Makes sense to me 👍

@pgaskin

pgaskin commented Aug 27, 2025

Copy link
Copy Markdown
Member

avoid rewriting the whole player

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.

@ihy123

ihy123 commented Aug 28, 2025

Copy link
Copy Markdown
Contributor Author

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)

@ihy123

ihy123 commented Aug 28, 2025

Copy link
Copy Markdown
Contributor Author

I've been meaning to myself for a while.

I bet threads are a significant barrier actually. You're probably like:

  • open player.c
  • look at 1000+ lines of multithreaded code
  • <swear words>
  • close player.c

That's why I don't like them. 1000+ lines of single-threaded code would be much better

@flyingmutant

Copy link
Copy Markdown
Member

Are there any popular distributions that ship FFmpeg versions that will break build after 07e4c10?

Comment thread ip/ffmpeg.c

res = avcodec_send_packet(priv->codec_ctx, priv->pkt);
if (res == AVERROR(EAGAIN))
if (res == 0 || res == AVERROR(EAGAIN))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res == 0 means success, how is it related to empty frames?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread ip/ffmpeg.c Outdated
*out_sample_fmt = cc->sample_fmt;
switch (*out_sample_fmt) {
case AV_SAMPLE_FMT_U8:
sf |= sf_bits(8) | sf_signed(0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
 */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also s16 is better resolution for applying replaygain and volume, there will be less distortion

@pgaskin

pgaskin commented Aug 30, 2025

Copy link
Copy Markdown
Member

Are there any popular distributions that ship FFmpeg versions that will break build

no: https://repology.org/badge/vertical-allrepos/ffmpeg.svg

@flyingmutant
flyingmutant merged commit d73726d into cmus:master Sep 2, 2025
@BoostCookie

Copy link
Copy Markdown

I have just tried it out and I can confirm that this indeed fixes #1397.
Thank you so much for the marvellous work!

@ihy123
ihy123 deleted the ffmpeg branch September 9, 2025 15:14
@BrewTestBot BrewTestBot mentioned this pull request Oct 28, 2026
1 task done
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request Nov 2, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No gapless playback and bad seeking for mka (matroska) file with cue sheet.

5 participants