Opened 15 months ago

Last modified 8 months ago

#10465 open defect

The latest versions of ffmpeg (>=5.x) produces not playable on Apple devices .caf files

Reported by: Sergey Owned by:
Priority: normal Component: avformat
Version: git-master Keywords: opus caf ios macos
Cc: Sergey Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Source file contains opus audio stream inside, and it's playable on iOs and Mac OS.
But if do

% ffmpeg -y -i playable.caf -c:a copy converted.caf

(i.e. I just trying to duplicate content of audio stream to another file) I get not playable .caf file.

After some investigation I found result file has mFramesPerPacket (from struct CAFAudioFormat: https://developer.apple.com/library/archive/documentation/MusicAudio/Reference/CAFSpec/CAF_spec/CAF_spec.html) different from 960 (comparing to playable.caf). If I change that piece of file to 960 - both iOs and Mac OS starts play this file without any issue.

After further investigation I found problem is on https://github.com/FFmpeg/FFmpeg/blob/75b1a555a70c178a9166629e43ec2f6250219eb2/libavformat/cafenc.c#L262

packet_size = st->duration / (caf->packets - 1);

And if I replace that line to

packet_size = st->duration / caf->packets;

it fixes output file, but I'm not sure if it's right fix.

Attachments (2)

playable.caf (5.9 KB ) - added by Sergey 15 months ago.
0001-avformat-cafenc-fixed-packet_size-calculation.patch (1.8 KB ) - added by Sergey 12 months ago.
possible issue fix

Download all attachments as: .zip

Change History (11)

by Sergey, 15 months ago

Attachment: playable.caf added

comment:2 by Sergey, 15 months ago

Version: 5.0.3git-master

comment:3 by Sergey, 12 months ago

I think I've found the reason. And it's very simple, even if audio stream has constant packet_size for all containing packets, it's almost always the last one is incomplete (i.e. less than packet_size) since there is no enough audio data to fill it completely, and it leads to wrong calculations. And it's possible using of (caf->packets - 1) in packet_size calculations was try to fix it. But for complete fix it's required to subtract duration of the last (incomplete) packet from full audio stream duration.

Also, maybe, it worth put calculation of duration directly to cafenc.c (if be more specific into caf_write_packet function) - it will give more control over calculations.

Anyway, I'm going to prepare patch for this issue...

Last edited 12 months ago by Sergey (previous) (diff)

by Sergey, 12 months ago

possible issue fix

comment:4 by Sergey, 12 months ago

I've provided possible issue fix, but it's highly possible it's too naive (or just totally wrong), since I'm not very familiar with ffmpeg internals. So I will be very glad to any comment or critique.

Thanks in advance.

comment:5 by Sergey, 8 months ago

Cc: Sergey removed

comment:6 by Sergey, 8 months ago

Cc: Sergey added

comment:7 by Sergey, 8 months ago

@mkver do you know by chance who can review patches to this part of ffmpeg?

comment:8 by Sergey, 8 months ago

Owner: set to mkver
Status: newopen

comment:9 by mkver, 8 months ago

Owner: mkver removed
Note: See TracTickets for help on using tickets.