From: Bex Kelly <bk@ancilla.ca> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] libavformat/libgme: avoid infinite reads of looping files Date: Tue, 16 Jan 2024 13:15:11 -0500 Message-ID: <20240116181524.2682575-1-bk@ancilla.ca> (raw) From: Rebecca Kelly <bk@ancilla.ca> This fixes an issue where certain VGM/VGZ files (and possibly other formats handled by libgme) will report a duration of "N/A" when ffprobed, and will produce an infinite-length audio file when converted with ffmpeg. The behaviour here follows the recommendations in the libgme documentation¹: use total duration if available, otherwise use intro length + two loops, otherwise pretend the file is 2.5 minutes long. It uses gme_set_fade() to implement this, which will cause libgme to insert a clean fade-out and then report end of file at the specified time. Patch released under the LGPL 2.1 license (or any later version). ——— ¹ https://github.com/mcfiredrill/libgme/blob/master/gme.txt Signed-off-by: Rebecca Kelly <btk@google.com> --- libavformat/libgme.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/libavformat/libgme.c b/libavformat/libgme.c index 4d04537339..b2635fc89f 100644 --- a/libavformat/libgme.c +++ b/libavformat/libgme.c @@ -63,7 +63,26 @@ static int load_metadata(AVFormatContext *s, int64_t *duration) if (gme_track_info(gme->music_emu, &info, gme->track_index)) return AVERROR_STREAM_NOT_FOUND; - *duration = info->length; + // Some formats support looped audio. In these cases, length will be -1, and + // the track length needs to be determined from intro_length and loop_length + // instead. Furthermore, some formats have no length data at all, in which + // case all of these fields will be -1. + // When reading these formats, GME will return audio frames forever if you + // ask for them; it's up to the caller to decide when to stop, either by + // closing the file after a certain point or by calling gme_set_fade() + // before starting playback, which will cause GME to simulate a fadeout and + // end of file at that point. + // The GME documentation recommends a default behaviour of playing the intro + // followed by two loops, for looped audio, or playing 2.5 minutes and then + // fading out, for audio of indeterminate length. + if (info->length > 0) { + *duration = info->length; + } else if (info->intro_length > 0 || info->loop_length > 0) { + *duration = info->intro_length + info->loop_length * 2; + } else { + *duration = 150000; // 2.5 minutes in milliseconds + } + add_meta(s, "system", info->system); add_meta(s, "game", info->game); add_meta(s, "song", info->song); @@ -138,8 +157,10 @@ static int read_header_gme(AVFormatContext *s) if (!st) return AVERROR(ENOMEM); avpriv_set_pts_info(st, 64, 1, 1000); - if (duration > 0) + if (duration > 0) { st->duration = duration; + gme_set_fade(gme->music_emu, duration); // Avoid infinite playback + } st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; st->codecpar->codec_id = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE); st->codecpar->ch_layout.nb_channels = 2; -- 2.42.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
reply other threads:[~2024-01-16 18:16 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20240116181524.2682575-1-bk@ancilla.ca \ --to=bk@ancilla.ca \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git