fix: preserve audio sample continuity across timestamp rounding

This commit is contained in:
2026-10-10 01:26:26 +02:00
parent 8a89d88254
commit df2456f559
5 changed files with 137 additions and 4 deletions
+8 -2
View File
@@ -404,6 +404,7 @@ struct Player {
}
void convertAudio() {
const bool continuing = resampler != nullptr;
auto &track = tracks[0];
const int rate = decoded->sample_rate > 0 ? decoded->sample_rate : track.codec->sample_rate;
const AVChannelLayout *layout = decoded->ch_layout.nb_channels > 0 ?
@@ -424,8 +425,13 @@ struct Player {
throw std::runtime_error("Audio format changes within a track are not supported");
}
const double timestamp = framePts(track, decoded);
track.nextPts = timestamp + static_cast<double>(decoded->nb_samples) / rate;
pcmPts = timestamp - static_cast<double>(swr_get_delay(resampler, rate)) / rate;
const double tick = av_q2d(format->streams[track.index]->time_base);
const bool contiguous = continuing && std::abs(timestamp - track.nextPts) <= tick + 1.0 / rate;
// Quantized packet PTS and rounded resampler delay must not insert/drop PCM samples.
const int64_t delayBase = static_cast<int64_t>(rate) * sampleRate;
pcmPts = contiguous ? resampleNextPts :
timestamp - static_cast<double>(swr_get_delay(resampler, delayBase)) / delayBase;
track.nextPts = (contiguous ? track.nextPts : timestamp) + static_cast<double>(decoded->nb_samples) / rate;
const int capacity = swr_get_out_samples(resampler, decoded->nb_samples);
check(capacity, "Sizing resampled audio");
if (capacity > audioFrameLimit)